使用Spring AOP时出现@ autowired依赖无法注入如何解决

  介绍

这篇文章给大家介绍使用Spring AOP时出现@ autowired依赖无法注入如何解决,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<强>发现问题:

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

<强>解决过程:

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

public  static  boolean 卡纳普(Pointcut  pc, Class  targetClass,, boolean  hasIntroductions), {,   (!,if  pc.getClassFilter () .matches (targetClass)), {,   ,,return 假;   }大敌;   ,   ,MethodMatcher  MethodMatcher =, pc.getMethodMatcher (),,   ,IntroductionAwareMethodMatcher  IntroductionAwareMethodMatcher =,零,,   ,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 =,(类),it.next (),,   方法[],才能methods =, clazz.getMethods (),,   for 才能;(int  j =, 0;, j  & lt;, methods.length;, j + +), {,   ,,if  (introductionAwareMethodMatcher  !=, null ,,,   ,,,,introductionAwareMethodMatcher.matches(方法[j], targetClass,, hasIntroductions)), | |,   ,,,,methodMatcher.matches(方法[j], targetClass)), {,   ,,,return ,真的,,   ,,},   ,,},   }大敌;   ,   ,return 假;   }

此处<代码>方法[]方法=clazz.getMethods();>

<代码>执行(* *(. .))>

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

<强>修正办法:

,,,,1,将方法修饰符改为公众;

,,,,2、使用AspectJ来进行注入。

关于使用Spring AOP时出现@ autowired依赖无法注入如何解决就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。

使用Spring AOP时出现@ autowired依赖无法注入如何解决