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...

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM