SpringAOP导致@Autowired依赖注入失败
之前用springAOP做了个操作日志记录,这次在往其他类上使用的时候,service一直注入失败,找了网上好多内容,发现大家都有类似的情况出现,但是又和自己的情况不太符合。后来总结自己的情况发现:方法为private修饰的,在AOP适配的时候会导致service注入失败,并且同一个service在其他的public方法中就没有这种情况,十分诡异。
结合查阅的资料进行了分析:在org.springframework.aop.support.AopUtils中:
public static boolean canApply(Pointcut pc, Class targetClass, boolean hasIntroductions) {
if (!pc.getClassFilter().matches(targetClass)) {
return false;
}
MethodMatcher methodMatcher = pc.getMethodMatcher();
IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
}
Set classes = new HashSet(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
classes.add(targetClass);
for (Iterator it = classes.iterator(); it.hasNext();) {
Class clazz = (Class) it.next();
Method[] methods = clazz.getMethods();
for (int j = 0; j < methods.length; j++) {
if ((introductionAwareMethodMatcher != null &&
introductionAwareMethodMatcher.matches(methods[j], targetClass, hasIntroductions)) ||
methodMatcher.matches(methods[j], targetClass)) {
return true;
}
}
}
return false;
}
此处Method[] methods = clazz.getMethods();只能拿到public方法。
execution(* *(..)) 可以匹配public/protected的,因为public的有匹配的了,目标类就代理了,,,再进行切入点匹配时也是能匹配的,而且cglib方式能拿到包级别/protected方法,而且包级别/protected方法可以直接通过反射调用。
private 修饰符的切入点 无法匹配 Method[] methods = clazz.getMethods(); 这里的任何一个,因此无法代理的。 所以可能因为private方法无法被代理,导致@Autowired不能被注入。
修正办法:
1、将方法修饰符改为public;
2、使用AspectJ来进行注入。
SpringAOP导致@Autowired依赖注入失败的更多相关文章
- 解决 Springboot中Interceptor拦截器中依赖注入失败
问题: 在Springboot拦截器Interceptor中使用@Resource依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去 原配置为: @Configurationpubli ...
- @Autowired自动注入失败
新手注意的问题 package cn.ryq.web.controller; import cn.ryq.domain.company.Company;import cn.ryq.service.co ...
- SpringMVC的filter怎么使用Autowired依赖注入bean
有的时候根据我们业务的需要,我们需要在web项目中定义一个自己的filter,并想在这个filter中使用@Autowired注入bean供我们使用.如果直接使用的话是不行的,需要我们在xml文件 ...
- @Autowired注解注入失败,提示could not autowire的解决办法
autowire异常主要由三个情况发生的 像上面的情况是BrandDao没有注入, 1.你的BrandServiceImpl必须以@Service或@Component注解才行. 2.自动写入的时候把 ...
- Springboot 拦截器 依赖注入失败
解决方案2种. ====1 https://blog.csdn.net/shunhua19881987/article/details/78084679 ====2 https://www.cnblo ...
- 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案
错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...
- Spring 依赖注入优化
Spring 依赖注入优化 原创: carl.zhao SpringForAll社区 今天 Spring 最大的好处就是依赖注入,关于什么是依赖注入,在Stack Overflow上面有一个问题,如何 ...
- 一文搞懂│php 中的 DI 依赖注入
目录 什么是 DI / 依赖注入 依赖注入出现的原因 简单的依赖注入 高阶的依赖注入 依赖注入的应用 依赖注入高阶优化 什么是 DI / 依赖注入 依赖注入DI 其实本质上是指对类的依赖通过构造器完成 ...
- static类型autowired 注入失败
原代码:注入commonService对象失败 @Autowired private static CommonService commonService; public static List< ...
随机推荐
- OCP-1Z0-051-题目解析-第7题
7. Which two statements are true regarding the USING and ON clauses in table joins? (Choose two.) A ...
- 基于C++11线程池
1.包装线程对象 class task : public std::tr1::enable_shared_from_this<task> { public: task():exit_(fa ...
- css3动画实例测试
1.css3动画属性分析(2016-5-11) 1.transition: 规定属性变换规则,可以这样讲.transition(a,b,c,d); a:要变换的属性: b:过渡时间: c:运动方式: ...
- 【转】Android学习系列–App离线下载功能实现
原文:http://www.cnblogs.com/qianxudetianxia/archive/2011/07/20/2108965.html 宜未雨而绸缪,毋临渴而掘井.----朱用纯<治 ...
- 自动生成api文档
vs2010代码注释自动生成api文档 最近做了一些接口,提供其他人调用,要写个api文档,可是我想代码注释已经写了说明,能不能直接把代码注释生成api?于是找到以下方法 环境:vs2010 先下载安 ...
- innerText与innerHTML的区别
innerText与innerHTML的区别:1.innerText将所有文本内容作为普通的文本2.innerHTML会识别文本内容中是否含有html标签,它能够把html标签的效果显示出来3.inn ...
- CSS绘制无图片的气泡对话框
<div class="qipao_contianer"> <div class="qipao_content yj3" ...
- 利用Matlab生成一个网格化的三维球面(生成直角坐标)
利用Matlab生成一个网格化的三维球面,分别对径向方向.经度方向和纬度方向进行网格化,代码如下: %生成一个笛卡尔坐标系下球面网格的x,y,z坐标 %r为球面距离 %nJingdu,nWeidu分别 ...
- Visual Studio 2013 Use HTTPS (SSL) On Web Application Projects
公司调试HTTPS接口会用到,原文:http://www.codeproject.com/Tips/766918/Visual-Studio-Use-HTTPS-SSL-On-Web-Applicat ...
- .net开发框架设计
转WisDom .net开发框架设计 WisDom .net 框架设计 1. 为啥要弄 2014 年我已经是我们参加工作的第六年,也做过不少项目,但是发现自己没有代码积累.这里利用业余时间梳理一下 ...