Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解
一、切面的優(yōu)先級
①創(chuàng)建類ValidateAspect:
由于要把我們的切面類和我們的目標類來進行ioc容器的一個組件,所以我們需要加上@Component注解,然后由于我們要把當前切面類來標識為一個組件,我們需要@Aspect注解
切面的優(yōu)先級:
可以通過@Order注解的value屬性設置優(yōu)先級,默認值為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)(了解)
①復制基于注解的AOP實現(xiàn)的四個接口和類
②刪除@Aspect注解(將組件標識為切面),切入點表達式的注解@Pointcut,把方法標識為通知方法的注解@Before...,@Order注解
③創(chuàng)建xml文件
<!--掃描組件-->
<context:component-scan base-package="com.tian.spring.aop.xml"></context:component-scan>
<aop:config>
<!--設置一個公共的切入點表達式-->
<aop:pointcut id="pointCut" expression="execution(* com.tian.spring.aop.xml.CalculatorImpl.*(..))"/>
<!--將IOC容器中的某個bean設置為切面-->
<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);
}
}到此這篇關于Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解的文章就介紹到這了,更多相關Spring切面優(yōu)先級內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何使用JDBC連接數(shù)據(jù)庫并執(zhí)行SQL語句
JDBC是Java數(shù)據(jù)庫連接的縮寫,是Java程序與數(shù)據(jù)庫進行交互的標準API。JDBC主要包括Java.sql和javax.sql兩個包,通過DriverManager獲取數(shù)據(jù)庫連接對象Connection,并通過Statement或PreparedStatement執(zhí)行SQL語句2023-04-04
IntelliJ IDEA配置Tomcat(完整版圖文教程)
這篇文章主要介紹了IntelliJ IDEA配置Tomcat(完整版圖文教程),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題
這篇文章主要介紹了springboot集成druid,多數(shù)據(jù)源可視化,p6spy問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
利用Spring Social輕松搞定微信授權登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權登錄的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

