Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解
一、切面的優(yōu)先級
①創(chuàng)建類ValidateAspect:
由于要把我們的切面類和我們的目標(biāo)類來進行ioc容器的一個組件,所以我們需要加上@Component注解,然后由于我們要把當(dāng)前切面類來標(biāo)識為一個組件,我們需要@Aspect注解
切面的優(yōu)先級:
可以通過@Order注解的value屬性設(shè)置優(yōu)先級,默認(rèn)值為Integer的最大值
@Order注解的value屬性值越小,優(yōu)先級越高
@Component @Aspect @Order(1) public class ValidateAspect { // @Before("execution(* com.tian.spring.aop.annotation.CalculatorImpl.*(..))") @Before("com.tian.spring.aop.annotation.LoggerAspect.pointCut()") public void beforeMethod() { System.out.println("ValidateAspect-->前置通知"); } }
②測試類:
public class AOPTest { @Test public void testAOPByAnnotation() { ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-annotation.xml"); Calculator calculator = ioc.getBean(Calculator.class); calculator.div(10,1); } }
二、基于xml的AOP實現(xiàn)(了解)
①復(fù)制基于注解的AOP實現(xiàn)的四個接口和類
②刪除@Aspect注解(將組件標(biāo)識為切面),切入點表達(dá)式的注解@Pointcut,把方法標(biāo)識為通知方法的注解@Before...,@Order注解
③創(chuàng)建xml文件
<!--掃描組件--> <context:component-scan base-package="com.tian.spring.aop.xml"></context:component-scan> <aop:config> <!--設(shè)置一個公共的切入點表達(dá)式--> <aop:pointcut id="pointCut" expression="execution(* com.tian.spring.aop.xml.CalculatorImpl.*(..))"/> <!--將IOC容器中的某個bean設(shè)置為切面--> <aop:aspect ref="loggerAspect"> <aop:before method="beforeAdviceMethod" pointcut-ref="pointCut"></aop:before> <aop:after method="afterAdviceMethod" pointcut-ref="pointCut"></aop:after> <aop:after-returning method="afterReturningAdviceMethod" returning="result" pointcut-ref="pointCut"></aop:after-returning> <aop:after-throwing method="afterThrowingAdvice" throwing="ex" pointcut-ref="pointCut"></aop:after-throwing> <aop:around method="aroundAdviceMethode" pointcut-ref="pointCut"></aop:around> </aop:aspect> <aop:aspect ref="validateAspect" order="1"> <aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before> </aop:aspect> </aop:config>
④測試類:
public class AOPByXMLTest { @Test public void testAOPByXML() { ApplicationContext ioc = new ClassPathXmlApplicationContext("aop-xml.xml"); Calculator calculator = ioc.getBean(Calculator.class); calculator.add(1,2); } }
到此這篇關(guān)于Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Spring切面優(yōu)先級內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java環(huán)境變量的配置方法圖文詳解【win10環(huán)境為例】
這篇文章主要介紹了java環(huán)境變量的配置方法,結(jié)合圖文形式詳細(xì)分析了win10環(huán)境下java環(huán)境變量的配置方法與相關(guān)操作注意事項,需要的朋友可以參考下2020-04-04Java郵件發(fā)送程序(可以同時發(fā)給多個地址、可以帶附件)
不錯的功能比較齊全的郵件發(fā)送程序源碼2008-07-07SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11Java單線程程序?qū)崿F(xiàn)實現(xiàn)簡單聊天功能
這篇文章主要介紹了Java單線程程序?qū)崿F(xiàn)實現(xiàn)簡單聊天功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10postgresql 實現(xiàn)16進制字符串轉(zhuǎn)10進制數(shù)字
這篇文章主要介紹了postgresql 實現(xiàn)16進制字符串轉(zhuǎn)10進制數(shù)字操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Mybatis配置之properties和settings標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之properties和settings標(biāo)簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07