AOP:在spring的三種配置方式

AOP(Aspect Oriented Programming)面向切面編程

  • 可以通過(guò)預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)在不修改源代碼的情況下給程序動(dòng)態(tài)統(tǒng)一添加功能的一種技術(shù),實(shí)現(xiàn)橫切關(guān)注點(diǎn)(如日志、安全、緩存和事務(wù)管理)與他們所影響的對(duì)象之間的解耦。
  • AOP在spring的三種配置方式

1.XML:自動(dòng)代理

    <!--被代理對(duì)象(target)-->
    <bean id="orderServiceTarget" class="com.apesource.service.Impl.OrderServiceImpl"/>
    <!--通知(Advice)-->
    <bean id="logAdviceBean" class="com.apesource.advice.LogAdvice"/>
    <bean id="PerformanceAdviceBean" class="com.apesource.advice.PerformanceAdvice"/>

    <!--切入點(diǎn):通過(guò)正則表達(dá)式描述指定切入點(diǎn)(某些指定方法)-->
    <bean id="createMethodPointcutBean"                           class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <!--注入正則表達(dá)式:描述哪些方法為切入點(diǎn)-->
        <property name="pattern" value=".*creat.*"/>
    </bean>
    <!--Advisor(高級(jí)通知)=Advice(通知)+Pointcut(切入點(diǎn))-->
    <bean id="performanceAdvisorBean" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <!--注入切入點(diǎn)-->
        <property name="pointcut" ref="createMethodPointcutBean"/>
        <!--注入通知-->
        <property name="advice" ref="PerformanceAdviceBean"/>
    </bean>
    <!--自動(dòng)代理創(chuàng)建-->
    <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <!--Bean名稱規(guī)則(數(shù)組):指定哪些bean創(chuàng)建自動(dòng)代理-->
        <property name="beanNames">
            <list>
                <value>*ServiceTarget</value>
            </list>
        </property>
        <!--通知列表:需要執(zhí)行哪些通知-->
        <property name="interceptorNames">
            <list>
                <value>performanceAdvisorBean</value>
                <value>logAdviceBean</value>
            </list>
        </property>
    </bean>

2.AspectJ:在類中自定義通知,在xml文件中配置切面

<!--業(yè)務(wù)組件bean-->
    <bean id="orderServiceBean" class="com.apesource.service.Impl.OrderServiceImpl"/>
    <!--日志Aspect切面-->
    <bean id="logAspectBean" class="com.apesource.advice.LogAspect"/>

    <!--使用Aspect實(shí)現(xiàn)切面,使用Spring AOP進(jìn)行配置-->
    <aop:config>
        <!--配置切面-->
        <!--注入切面bean-->
        <aop:aspect ref="logAspectBean">
            <!--定義Pointcut:通過(guò)expression表達(dá)式,來(lái)查找特定的方法(pointcut)-->
            <aop:pointcut id="serviceMethodPointcut" expression="execution(* com.apesource.service.Impl.*.creat*(..) )"/>

            <!--配置“前置通知”-->
            <!--在Pointcut切入點(diǎn)()查找到的方法執(zhí)行“前”,執(zhí)行當(dāng)前l(fā)ogAspectBean的doBefore-->
            <aop:before method="beforeAdvice" pointcut-ref="serviceMethodPointcut"/>

            <!--配置“后置通知”-->
            <!--returning屬性:配置當(dāng)前方法中用來(lái)接收返回值的參數(shù)名-->
            <aop:after-returning returning="returnVal" method="afterReturningAdvice" pointcut-ref="serviceMethodPointcut"/>
            <aop:after method="afterAdvice" pointcut-ref="serviceMethodPointcut"/>

            <!--配置“異常通知”-->
            <!--throwing屬性:配置當(dāng)前方法中用來(lái)接收當(dāng)前異常的參數(shù)名-->
            <aop:after-throwing throwing="ex" method="throwingAdvice" pointcut-ref="serviceMethodPointcut"/>

            <!--配置環(huán)繞通知-->
            <aop:around method="aroundAdvice" pointcut-ref="serviceMethodPointcut"/>
        </aop:aspect>
    </aop:config>

3.注解:xml文件中配置自動(dòng)掃描器和AspectJ的自動(dòng)代理,在自定義的通知前加注解

    <!--自動(dòng)掃描器-->
    <context:component-scan base-package="com.apesource"/>
    <!--配置AspectJ的自動(dòng)代理-->
    <aop:aspectj-autoproxy/>
@Aspect()//標(biāo)注當(dāng)前類為切面類
@Before()//前置通知
@AfterReturning()//后置通知(有返回值)
@After()//后置通知
@AfterThrowing()//異常通知
@Around()//環(huán)繞通知

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容