【Spring學習筆記】3.spring AOP(XML版)

簡介

面向切面編程,編程的關注點是一個橫切面

實現過程

額外補充依賴

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.3</version>
    </dependency>

配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--AOP是基於代理完成的,所以要激活自動代理-->
    <aop:aspectj-autoproxy/>

    <!--註冊一個切面要使用的類-->
    <bean class="com.cyk.advice.BeforceAdvice" id="beforceAdvice">
    </bean>

    <bean class="com.cyk.service.ProviderService" id="providerService">
    </bean>

    <!--配置切入點等信息-->
    <aop:config>
        <aop:aspect id="beforeAspect" ref="beforceAdvice">
            <!--aop:before 表明使用前置通知
                method指明它用哪個方法來切
                poincut切入點 路徑名+方法名
            -->
            <!--如果有多個同類型的建議,誰在前誰先執行-->
            <aop:before method="methodBefore" pointcut="execution(* com.cyk.service.*.*(..))"/>
            <aop:before method="before" pointcut="execution(* com.cyk.service.*.*(..))"/>
        </aop:aspect>
    </aop:config>


</beans>

</beans>

使用returning對返回值進行綁定

public void afterReturning(String returning){
        System.out.println("返回值是:"+ returning);
    }
<bean class="com.cyk.advice.AfterReturningAdvice" id="afterReturningAdvice">
    </bean>
<aop:aspect ref="afterReturningAdvice">
            <aop:after-returning method="afterReturning" pointcut="execution(* com.cyk.service.*.*(..))" returning="returning"/>
        </aop:aspect>

ExceptionAdvice

public void exe(){
        System.out.println("執行exe");
        throw new RuntimeException("我故意的")
    }
<bean class="com.cyk.advice.ExceptionAdvice" id="exceptionAdvice">
    </bean>
<aop:aspect ref="exceptionAdvice">
            <aop:after-throwing method="excep" pointcut="execution(* com.cyk.service.*.*(..))"/>
        </aop:aspect>

AroundAdvice

  • 既可以在目標方法之前織入增強動作,也可以在執行目標方法之後織入增強動作;
  • 它可以決定目標方法在什麼時候執行,如何執行,甚至可以完全阻止目標目標方法的執行;
  • 它可以改變執行目標方法的參數值,也可以改變執行目標方法之後的返回值;
  • 當需要改變目標方法的返回值時,只能使用Around方法;

雖然Around功能強大,但通常需要在線程安全的環境下使用。因此,如果使用普通的Before、AfterReturing增強方法就可以解決的事情,就沒有必要使用Around增強處理了。

public Object around(ProceedingJoinPoint pjp) {

        try {
            System.out.println("環繞地球80天。。。。。。。。。。。。。。。。。。。。。");
            Object proceed = pjp.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }

    }
<bean class="com.cyk.advice.AroundAdvice" id="aroundAdvice">
    </bean>
<aop:aspect ref="aroundAdvice">
            <aop:around method="around" pointcut="execution(* com.cyk.service.*.*(..))"/>
        </aop:aspect>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章