spring 中使用 @Aspect 注解实现 aop 切面

1、创建切面类

@Component
@Aspect
public class LogAspect {

    @Before(value = "execution(* com.cl.service..*.*(..))")
    public void before(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ before ]"+name+"执行");
    }

    @After(value = "execution(* com.cl.service..*.*(..))")
    public void after(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ after ]"+name+"执行");
    }

    @AfterReturning(value = "execution(* com.cl.service..*.*(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ afterReturning ]"+name+"方法的结果为:"+result);
    }

    @AfterThrowing(value = "execution(* com.cl.service..*.*(..))",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println("[ afterReturning ]");
        exception.printStackTrace();
    }
}
  • @Aspecet 注解 将当前类标记为切面类
  • @Component注解 通过包扫描,将含有此标签的类注入到 IoC

2、配置 bean.xml 文件

<?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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 扫描 @component、@Repository、@service、@control 等注解,扫描到了之后自动添加到 IoC 容器中 -->
    <context:component-scan base-package="com.cl"></context:component-scan>

    <!-- 开启 @aspect 注解 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

3、编写业务代码并测试

  • 业务接口
public interface IRandomTest {
    int runTest();
}
  • 业务接口实现
@Component
public class RandomTest implements IRandomTest {
    public int runTest() {
        System.out.println("执行了业务代码");
        return 4;
    }
}
  • 测试类
public class Text {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
        IRandomTest randomTest = (IRandomTest) applicationContext.getBean("randomTest");
        randomTest.runTest();
    }
}

注意点: 因为业务接口实现类使用了 @Component 注解包扫描会扫描到这个注解,它会将 RandomTest 自动注入到 IoC 容器中,所以可以在测试类中直接到 IoC 容器中取出 bean 。

上一篇:Reactor响应式编程 记录web访问日志 filter+AOP


下一篇:linux上的socket通信