IDEA下使用AOP

2021-01-12 10:16:53 浏览数 (1)

1、创建两个类User(被增强类)和UserProxy(增强类)

User类

代码语言:java复制
import org.springframework.stereotype.Component;
@Component
public class User {
    public void add(){
        System.out.println("add.....");
    }
}

UserProxy类

代码语言:javascript复制
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//增强的类
@Component
@Aspect
public class UserProxy {
    @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void pointdemo(){

    }
    //前置通知 value的值为要增强的对象
    @Before(value = "pointdemo()")
    public void before(){
        System.out.println("before.....");
    }
    //最终通知 有无异常都执行
    @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void after(){
        System.out.println("after");
    }
    //后置通知(或返回通知)  有异常的时候不执行,执行的是afterThrowing
    @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterReturning(){
        System.out.println("afterreturning...");
    }
    //异常通知
    @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing...");
    }
    //环绕通知
    @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后");
    }

}

2、编写配置文件XXX.xml存放在resource文件夹中

代码语言:javascript复制
<?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:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启组件扫描-->
    <context:component-scan base-package="com.atguigu.spring5.aopanno"></context:component-scan>

    <!--开启AspectJ生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

3、编写测试类

代码语言:javascript复制
import com.atguigu.spring5.aopanno.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    @Test
    public void testAopAnno(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        User user =  context.getBean("user", User.class);
        user.add();
    }
}

4、测试结果

代码语言:javascript复制
环绕之前
before.....
add.....
afterreturning...
after
环绕之后

Process finished with exit code 0

0 人点赞