SpringAOP导致@Autowired依赖注入失败
来源:互联网 发布:淘宝评价语100字 编辑:程序博客网 时间:2024/06/07 04:49
同事代码出现了一个诡异的问题,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来进行注入
原文链接:http://www.cnblogs.com/lcngu/p/6246950.html
阅读全文
0 0
- SpringAOP导致@Autowired依赖注入失败
- spring @Autowired注入失败
- 利用springaop导致基于实现类注入失败的原因及解决方案
- Spring中的@Autowired依赖注入
- Spring 依赖注入 @Autowired @Inject
- @Autowired注入dao时失败
- spring @Autowired 自动注入 失败
- Spring源码分析--@Autowired注入的不是代理对象,导致事务回滚失败(@Transactional无效)
- (项目)报错:Injection of autowired dependencies failed;依赖注入失败
- Spring依赖注入之@Inject,@Autowired,@Resource
- spring 中@Autowired注入失败问题
- Shiro Realm @Autowired 注入失败的问题
- Spring MVC 启动异常,@Autowired注入失败。
- springmvc关于@Autowired注入失败的问题
- Spring 依赖注入失败原因
- spring依赖注入失败原因
- Spring 注解实现Bean依赖注入之@Autowired
- 依赖注入导致的空指针异常
- gst-rtsp-server 转发服务器的搭建
- haproxy检测页面参数解释
- Leetcode(W9):123. Best Time to Buy and Sell Stock III(动态规划)
- 用jquery插件写一个小米官网左侧二级菜单
- 上拉刷新下拉加载
- SpringAOP导致@Autowired依赖注入失败
- C++ vector用法总结
- Invert a Binary Tree (25)
- python 聊天机器人 ,编译为exe
- 素数对猜想
- 1017. A除以B (20)
- 字符数组--ISBN号码
- 朴素贝叶斯学习笔记原理部分(NaiveBayesianModel)
- C++