SpringAOP导致@Autowired依赖注入失败

  之前用springAOP做了个操作日志记录,这次在往其他类上使用的时候,service一直注入失败,找了网上好多内容,发现大家都有类似的情况出现,但是又和自己的情况不太符合。后来总结自己的情况发现:方法为private修饰的,在AOP适配的时候会导致service注入失败,并且同一个service在其他的public方法中就没有这种情况,十分诡异。

  结合查阅的资料进行了分析:在org.springframework.aop.support.AopUtils中:

 1 public static boolean canApply(Pointcut pc, Class targetClass, boolean hasIntroductions) {  
 2     if (!pc.getClassFilter().matches(targetClass)) {  
 3         return false;  
 4     }  
 5   
 6     MethodMatcher methodMatcher = pc.getMethodMatcher();  
 7     IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;  
 8     if (methodMatcher instanceof IntroductionAwareMethodMatcher) {  
 9         introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;  
10     }  
11   
12     Set classes = new HashSet(ClassUtils.getAllInterfacesForClassAsSet(targetClass));  
13     classes.add(targetClass);  
14     for (Iterator it = classes.iterator(); it.hasNext();) {  
15         Class clazz = (Class) it.next();  
16         Method[] methods = clazz.getMethods();  
17         for (int j = 0; j < methods.length; j++) {  
18             if ((introductionAwareMethodMatcher != null &&  
19                     introductionAwareMethodMatcher.matches(methods[j], targetClass, hasIntroductions)) ||  
20                     methodMatcher.matches(methods[j], targetClass)) {  
21                 return true;  
22             }  
23         }  
24     }  
25   
26     return false;  
27 }  

  此处Method[] methods = clazz.getMethods();只能拿到public方法。

  execution(* *(..)) 可以匹配public/protected的,因为public的有匹配的了,目标类就代理了,,,再进行切入点匹配时也是能匹配的,而且cglib方式能拿到包级别/protected方法,而且包级别/protected方法可以直接通过反射调用。  

  private 修饰符的切入点 无法匹配 Method[] methods = clazz.getMethods(); 这里的任何一个,因此无法代理的。 所以可能因为private方法无法被代理,导致@Autowired不能被注入。

  修正办法:

1、将方法修饰符改为public;

2、使用AspectJ来进行注入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值