SrpingAOP编写

问题遇到的现象和发生背景

今天复习Spring AOP遇到一个诡异的问题,在没编写切面类类之前,xml的扫描包功能一切正常,当编写完切面类后,扫描包功能失效,一早上都没解决,在此提问。

问题相关代码,请勿粘贴截图

applicationContext.xml如下:


    
    <context:component-scan base-package="cn.edu.jyu"/>

    <aop:aspectj-autoproxy/>
package cn.edu.jyu.mapper.impl;

import cn.edu.jyu.mapper.Dao;
import org.springframework.stereotype.Repository;

@Repository
public class IDao implements Dao {
    @Override
    public void show() {
        System.out.println("---被增强的show方法---");
    }
}
运行结果及报错内容

本来一切正常,运行后控制台打印 被增强的show方法

但当编写完切面类后,运行报错,期间并为修改任何包、类名、路径等:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.edu.jyu.mapper.impl.IDao' available

切面类如下:

package cn.edu.jyu.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {

    /**
     * 前置通知 设置成mapper包下的所有子孙包下的所有类都能被增强
     * 其他包的方法没有被织入前置通知
     * @param join
     */
    @Before("execution(* cn.edu.jyu.mapper..*.*(..))")
    public void doBefore(JoinPoint join){
        System.out.println(join.getSignature().getName() + "---前置通知");
    }

    //异常通知
    @AfterThrowing("execution(* cn.edu.jyu.mapper..*.*(..))")
    public void doAfterThrowing(){
        System.out.println("异常通知");
    }

    @AfterReturning("execution(* cn.edu.jyu.mapper..*.*(..))")
    public void doAfterReturning(){
        System.out.println("后置通知");
    }

    /**
     * 后置通知切点表达式的访问修饰符设置成了 protected
     * public修饰符修饰的方法访问不到。
     */
    @After("execution(protected * cn.edu.jyu.mapper..*.*(..))")
    public void doAfter(){
        System.out.println("最终通知");
    }
}
我的解答思路和尝试过的方法

尝试过修改xml配置文件扫描包:


<context:component-scan base-package="cn.edu.jyu.mapper"/>

此时输出没有报错,但是切面方法显示 “This advice advises no methods”,而且想要的切入效果也没有。
如果改成:

    <context:component-scan base-package="cn.edu.jyu" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    context:component-scan>

运行结果再次报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.edu.jyu.mapper.impl.IDao' available

删除其中的:

include-filter type="annotation" expression="org.springframework.stereotype.Component"/>

运行结果再次正确

我想要达到的结果

我已经很迷了,之前完全没有遇到过这种情况,是我代码编写错误?还是扫描包用法错误?还是什么,求解答,谢谢。

spring的aop是使用的CGlib代理进行的动态代理,你设置为false就不会使用CGlib,这样就会导致aop出现问题,无法注册到spring容器,由于你的切面必须要spring容器管理才会生效,才能找到bean,所以你设置为FALSE,找不到bean,就会报错NoSuchBeanDefinitionException

可以看看这个:https://www.kuangstudy.com/zl/ssm#1381804434102726657

查了一天,终于知道哪里导致错误了,在applicationContext文件中:
<aop:aspectj-autoproxy proxy-target-class="true"/>
只知道设置成true是CGlib代理,但是为什么设置成false会导致其他bean无法注册?目前还不知道,希望有大神告知,不胜感激!