欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring切面優(yōu)先級(jí)與基于xml的AOP實(shí)現(xiàn)方法詳解

 更新時(shí)間:2022年11月09日 10:04:47   作者:學(xué)習(xí)使我快樂T  
這篇文章主要介紹了Spring切面的優(yōu)先級(jí)與基于xml的AOP的詳細(xì)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、切面的優(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)文章

最新評(píng)論