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)文章
新手學(xué)習(xí)Java對(duì)Redis簡(jiǎn)單操作
這篇文章主要介紹了新手學(xué)習(xí)Java對(duì)Redis簡(jiǎn)單操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
如何使用JDBC連接數(shù)據(jù)庫并執(zhí)行SQL語句
JDBC是Java數(shù)據(jù)庫連接的縮寫,是Java程序與數(shù)據(jù)庫進(jìn)行交互的標(biāo)準(zhǔn)API。JDBC主要包括Java.sql和javax.sql兩個(gè)包,通過DriverManager獲取數(shù)據(jù)庫連接對(duì)象Connection,并通過Statement或PreparedStatement執(zhí)行SQL語句2023-04-04
IntelliJ IDEA配置Tomcat(完整版圖文教程)
這篇文章主要介紹了IntelliJ IDEA配置Tomcat(完整版圖文教程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題
這篇文章主要介紹了springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Java的lambda表達(dá)式實(shí)現(xiàn)解析
這篇文章主要為大家詳細(xì)介紹了Java的lamda表達(dá)式實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
java設(shè)計(jì)模式之單例模式學(xué)習(xí)
單例對(duì)象(Singleton)是一種常用的設(shè)計(jì)模式。在Java應(yīng)用中,單例對(duì)象能保證在一個(gè)JVM中,該對(duì)象只有一個(gè)實(shí)例存在2014-01-01

