SpringAOP导致@Autowired依赖注入失败

简介: SpringAOP导致@Autowired依赖注入失败

用springAOP做操作日志记录,发现:方法为private修饰的,在AOP适配的时候会导致service注入失败,但是同一个service其他的public方法正常

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;
}

1. 这里的Method[] methods = clazz.getMethods();只能获得public方法。

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

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


解决:


方法修饰符为public;

使用AspectJ注入。

相关文章
|
1月前
|
Java Spring
Spring的Bean生命周期中@PostConstruct注解
【8月更文挑战第3天】在Spring框架中,`@PostConstruct`注解标示Bean初始化完成后立即执行的方法。它在依赖注入完成后调用,适用于资源加载、属性设置等初始化操作。若方法中抛出异常,可能影响Bean初始化。与之对应,`@PreDestroy`注解的方法则在Bean销毁前执行,用于资源释放。
|
4月前
|
Java Spring 容器
同一接口有多个实现类,怎么来注入一个指定的实现?@Resource、@Autowired、@Qualifier
同一接口有多个实现类,怎么来注入一个指定的实现?@Resource、@Autowired、@Qualifier
|
4月前
|
安全 Java 开发者
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
218 0
|
Java Spring 容器
springboot 静态方法中使用@Autowired注入配置和Bean
springboot 静态方法中使用@Autowired注入配置和Bean
|
开发框架 SpringCloudAlibaba Java
Spring注解装配:@Autowired和@Resource使用及原理详解
`@Resource`和`@Autowired`都是实现bean的注入,在日常开发中使用非常频繁,但是使用体验不太一样,笔者喜欢用`@Resource`,因为在使用`@Autowired`时IDEA会出现一些警告爆红提示
311 0
Spring注解装配:@Autowired和@Resource使用及原理详解
|
开发框架 Java Spring
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)
249 0
Spring - 属性注入之注解(@Autowired、@Qualifier、@Resource)
|
XML Java 数据格式
Spring - Bean管理之配置(@PostConstruct、@PreDestroy、@Scope)
Spring - Bean管理之配置(@PostConstruct、@PreDestroy、@Scope)
140 0
Spring - Bean管理之配置(@PostConstruct、@PreDestroy、@Scope)
|
开发框架 Java Spring
Spring中 如果该Service有多个实现类,它怎么知道该注入哪个ServiceImpl类?
如果该Service有多个实现类,它怎么知道该注入哪个ServiceImpl类?
通过实现ApplicationContextAware接口获取Bean
通过实现ApplicationContextAware接口获取Bean
141 0
|
XML Java 编译器
@Autowired 注解是如何实现的?
使用spring开发时,进行配置主要有两种方式,一是xml的方式,二是java config的方式
@Autowired 注解是如何实现的?