溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring如何基于XML實現Aop

發布時間:2021-07-19 09:11:47 來源:億速云 閱讀:165 作者:chen 欄目:開發技術

本篇內容介紹了“Spring如何基于XML實現Aop”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

目錄
  • 項目結構

  • 具體步驟

    • 1、創建maven 項目 導入依賴 創建好項目結構

    • 2、寫一個TestDao接口 及實現類

    • 3、編寫切面類

    • 測試


項目結構

Spring如何基于XML實現Aop

具體步驟

1、創建maven 項目 導入依賴 創建好項目結構

 <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

2、寫一個TestDao接口 及實現類

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:26
 */
public interface TestDao {
    public void sayHello();
    public void save();
    public void modify();
    public void delete();
}
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
public class TestDaoImpl implements TestDao {
    public void sayHello() {
        System.out.println("正在執行的方法-->武漢加油!中國加油!");
    }
    public void save() {
        System.out.println("正在執行的方法-->保存");
    }
    public void modify() {
        System.out.println("正在執行的方法-->修改");
    }
    public void delete() {
        System.out.println("正在執行的方法-->刪除");
    }
}

3、編寫切面類

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 17:12
 */
public class MyAspectXml {
    /**
     * 前置通知 使用JoinPoint 接口作為參數獲得目標對象的信息
     **/
    public void before(JoinPoint jp){
        System.out.print("前置通知:模擬權限控制   ");
        System.out.println("目標對象:"+jp.getTarget()+",被增強的方法:"+jp.getSignature().getName());
    }
    public void afterReturning(JoinPoint jp){
        System.out.print("后置返回通知:"+"模擬刪除臨時文件"  );
        System.out.println(",被增強的方法"+jp.getSignature().getName());
    }
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("環繞開始:執行目標方法前,模擬開啟事務");
        Object obj = pjp.proceed();
        System.out.println("環繞結束:執行目標方法后,模擬關閉事務");
        return obj;
    }
    public void except(Throwable throwable){
        System.out.println("異常通知:"+"程序執行異常"+throwable.getMessage());
    }
    public void after(){
        System.out.println("最終通知:釋放資源");
    }
}```
### 4、application.xml文件
```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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--
    <aop:aspectj-autoproxy />
    聲明自動為spring容器中那些配置@aspectJ切面的bean創建代理,織入切面。
    proxy-target-class屬性,默認為false,表示使用jdk動態代理織入增強,
    為true時: 表示使用CGLib動態代理技術織入增強。
    -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean id="testDaoImpl" class="com.dao.TestDaoImpl"/>
    <bean id="myAspectXML" class="com.aspect.MyAspectXml"/>
<!--    <bean id="myAspectXML2" class="com.aspect.MyAspectXml2"/>-->
    <!--
        補充:<aop:pointcut>如果位于<aop:aspect>元素中,則命名切點只能被當前<aop:aspect>內定義的元素訪問到,
        為了能被整個<aop:config>元素中定義的所有增強訪問,則必須在<aop:config>下定義切點。
    -->
    <aop:config>
        <!--切入點  execution 表達式 通過這個表達式篩選連接點 -->
        <aop:pointcut id="myPointCut" expression="execution(* com.dao.*.*(..))"/>
        <aop:aspect ref="myAspectXML">
			           <!--aop:after 是表示 這個方法是那種通知類型after 是方法之后     
           method="after" 這個after是切面類中的方法  -->
            <aop:after method="after" pointcut-ref="myPointCut"/>
            <aop:before method="before" pointcut-ref="myPointCut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/>
            <aop:after-throwing method="except" throwing="throwable" pointcut-ref="myPointCut"/>
            <aop:around method="around" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
</beans>

測試

@Test
    public void Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
        testDao.save();
        /**
         * 輸出:
         * 前置通知:模擬權限控制   目標對象:com.dao.TestDaoImpl@704f1591,被增強的方法:save
         * 環繞開始:執行目標方法前,模擬開啟事務
         * 正在執行的方法-->保存
         * 環繞結束:執行目標方法后,模擬關閉事務
         * 后置返回通知:模擬刪除臨時文件,被增強的方法save
         * 最終通知:釋放資源
         */
    }

“Spring如何基于XML實現Aop”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女