SSM的基于注解的aop失效

您看,当我点击运行的时候为什么没有织入呀
按照逻辑应该是先输出"我是前置通知"然后输出"输出了!"__

我的目标类Test1目标类有连接点show方法

package com.experience.test;
import org.springframework.stereotype.Repository;
import org.testng.annotations.Test;

/**
 * @author zhangsan
 */
@Repository
public class Test1 {
    @Test
    public void show(){
        System.out.println("输出了!");
    }
}


img

切入类和通知

package com.experience.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(void com.experience.test.Test1.show())")
    private void method(){}
    @Before("method() ")
    public void show(){
        System.out.println("我是前置通知!");
    }
}


img


配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--    开启注解扫描,扫描包-->
    <context:component-scan base-package="com.experience.service"/>
    <context:component-scan base-package="com.experience.test"/>
    <context:component-scan base-package="com.experience.aspect"/>
    <aop:aspectj-autoproxy/>
</beans>

img

img

img

因为你 Test1 并没有在 spring中作为bean被管理, 你的show方法是 单元测试方法, 并不是spring管理的类中的方法。

你Test1确实没有被spring bean去管理,aop动态代理自然就找不到你这个bean。自然aop切面无法起作用。
正确写法:
你应该写个controller类,然后调用service层,然后把切点切在service层方法。记得service类也需要被spring管理,加上@Service注解

你对切面的理解有问题,你想用注解切面,那你的注解没定义哇,另外,不管是测试还是正常项目,都需要放在spring容器管理,参考文章连接如下,我写的文章,springboot跟spring本质没啥区别,可以看看
https://hanyuhuai.blog.csdn.net/article/details/111829622

【以下回答由 GPT 生成】

答案:

根据你提供的代码和配置文件,没有发现明显的问题。在这种情况下,可以尝试以下几步来解决问题:

  1. 确保配置文件的位置和命名正确。在你的描述中,你提到你使用了Spring进行SSM开发,但是你只给出了一个Spring的配置文件,没有给出Mybatis和SpringMVC的配置文件。请确认你的项目中是否包含正确的配置文件,并且路径和文件名都是正确的。

  2. 确保切面(MyAspect)和目标类(Test1)被正确地扫描和管理。在Spring中,默认情况下使用@Component注解的类会被自动扫描并管理。请确保切面和目标类都是在@Component注解的扫描路径下,并且扫描路径配置正确。

  3. 确保切面和目标类都已经被正确地注入到Spring容器中。可以通过在切面和目标类中分别添加一个打印语句来验证。

切面类的修改:

package com.experience.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(void com.experience.test.Test1.show())")
    private void method(){}

    @Before("method()")
    public void show(){
        System.out.println("我是前置通知!");
    }

    public MyAspect(){
        System.out.println("MyAspect被实例化啦!");
    }
}

目标类的修改:

package com.experience.test;

import org.springframework.stereotype.Repository;
import org.testng.annotations.Test;

@Repository
public class Test1 {
    @Test
    public void show(){
        System.out.println("输出了!");
    }

    public Test1(){
        System.out.println("Test1被实例化啦!");
    }
}

重新运行应用程序,然后观察控制台输出。如果你没有看到"MyAspect被实例化啦!"和"Test1被实例化啦!"的输出,那说明切面和目标类没有被正确地注入到Spring容器中,请检查你的配置文件是否正确。

  1. 确保切入点表达式是正确的。在你的切入点表达式中,你使用了execution(void com.experience.test.Test1.show()),这表示切入的是Test1类中的show方法,返回类型为void。请确保你的切入点表达式是正确的,与目标方法的签名匹配。

如果经过上述步骤还未解决问题,那可能存在其他原因导致aop失效。比如可能在项目中使用了其他方式进行aop配置,导致冲突。在这种情况下,需要进一步排查其他可能的问题。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^