AOP 操作(AspectJ 注解)
1. 创建类,在类里面定义方法
1 2 3 4
| public class User { public void add() { System.out.println("add......."); } }
|
2. 创建增强类(编写增强逻辑)
1 2 3 4 5 6
|
public class UserProxy { public void before() { System.out.println("before......"); }
|
3. 进行通知的配置
- 在 spring 配置文件中,开启注解扫描
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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 basepackage="com.atguigu.spring5.aopanno"></context:component-scan>
|
- 使用注解创建 User 和 UserProxy 对象
1 2 3 4 5
| @Component public class User{} @Component public class UserProxy{}
|
- 在增强类上面添加注解 @Aspect
1 2 3 4
| @Component @Aspect public class UserProxy {
|
- 在 spring 配置文件中开启生成代理对象
1
| <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
|
- 配置不同类型的通知
在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| @Component @Aspect public class UserProxy { @Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void before() { System.out.println("before........."); } @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void afterReturning() { System.out.println("afterReturning........."); } @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void after() { System.out.println("after........."); } @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("环绕之后........."); } }
|
相同的切入点抽取
1 2 3 4 5 6 7 8 9 10
| @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") public void pointdemo() { }
@Before(value = "pointdemo()") public void before() { System.out.println("before........."); }
|
有多个增强类多同一个方法进行增强,设置增强类优先级
在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高
1 2 3 4
| @Component @Aspect @Order(1) public class PersonProxy
|
完全使用注解开发
创建配置类,不需要创建 xml 配置文件
1 2 3 4 5
| @Configuration @ComponentScan(basePackages = {"com.atguigu"}) @EnableAspectJAutoProxy(proxyTargetClass = true) public class ConfigAop { }
|
AOP 操作(AspectJ 配置文件)
- 创建两个类,增强类和被增强类,创建方法
- 在 spring 配置文件中创建两个类对象
1 2 3
| <bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean> <bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean>
|
- 在 spring 配置文件中配置切入点
1 2 3 4 5 6 7 8 9 10
| <aop:config> <aop:pointcut id="p" expression="execution(* com.atguigu.spring5.aopxml.Book.buy(..))"/> <aop:aspect ref="bookProxy"> <aop:before method="before" pointcut-ref="p"/> </aop:aspect> </aop:config>
|