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