Spring-AOP-增强类优先级-@Order注解
有多个增强类对同一个方法进行增强,使用@Order注解设置增强类优先级
(1)在增强类上面添加注解@Order(数字类型值),数字类型值越小优先级越高
1、被增强类
package com.orzjiangxiaoyu.aop.test2; import org.springframework.stereotype.Component; /** * @author orz * @create 2020-08-17 22:53 */ @Component public class Person { public void add() { System.out.println("Person add..."); } }
2、增强类一
package com.orzjiangxiaoyu.aop.test2; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author orz * @create 2020-08-17 22:54 */ @Component @Aspect @Order(5) public class PersonPonxy1 { @Before(value = "execution(* com.orzjiangxiaoyu.aop.test2.Person.add() )") public void before() { System.out.println("PersonPonxy1 add..."); } }
3、增强类二
package com.orzjiangxiaoyu.aop.test2; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; /** * @author orz * @create 2020-08-17 22:56 */ @Component @Aspect @Order(1) public class PersonPonxy2 { @Before(value = "execution(* com.orzjiangxiaoyu.aop.test2.Person.add() )") public void before() { System.out.println("PersonPonxy2 add..."); } }
4、配置文件
<?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:p="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开始组件扫面 --> <!-- 1.如果扫描多个包,多个包使用逗号隔开 2.扫描包上层目录 --> <context:component-scan base-package="com.orzjiangxiaoyu.aop"></context:component-scan> <!--开启Aspect生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
5、测试
package com.orzjiangxiaoyu.aop.test2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author orz * @create 2020-08-17 20:03 */ public class Test1 { @Test public void test2() { ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml"); Person person = context.getBean("person",Person.class); person.add(); } }
6、结果
PersonPonxy2 add...
PersonPonxy1 add...
Person add...