Spring基于XML實(shí)現(xiàn)Aop
項(xiàng)目結(jié)構(gòu)

具體步驟
1、創(chuàng)建maven 項(xiàng)目 導(dǎo)入依賴 創(chuàng)建好項(xiàng)目結(jié)構(gòu)
<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、寫一個(gè)TestDao接口 及實(shí)現(xiàn)類
/**
* @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("正在執(zhí)行的方法-->武漢加油!中國(guó)加油!");
}
public void save() {
System.out.println("正在執(zhí)行的方法-->保存");
}
public void modify() {
System.out.println("正在執(zhí)行的方法-->修改");
}
public void delete() {
System.out.println("正在執(zhí)行的方法-->刪除");
}
}
3、編寫切面類
/**
* @version 1.0
* @author: crush
* @date: 2021-03-10 17:12
*/
public class MyAspectXml {
/**
* 前置通知 使用JoinPoint 接口作為參數(shù)獲得目標(biāo)對(duì)象的信息
**/
public void before(JoinPoint jp){
System.out.print("前置通知:模擬權(quán)限控制 ");
System.out.println("目標(biāo)對(duì)象:"+jp.getTarget()+",被增強(qiáng)的方法:"+jp.getSignature().getName());
}
public void afterReturning(JoinPoint jp){
System.out.print("后置返回通知:"+"模擬刪除臨時(shí)文件" );
System.out.println(",被增強(qiáng)的方法"+jp.getSignature().getName());
}
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環(huán)繞開始:執(zhí)行目標(biāo)方法前,模擬開啟事務(wù)");
Object obj = pjp.proceed();
System.out.println("環(huán)繞結(jié)束:執(zhí)行目標(biāo)方法后,模擬關(guān)閉事務(wù)");
return obj;
}
public void except(Throwable throwable){
System.out.println("異常通知:"+"程序執(zhí)行異常"+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 />
聲明自動(dòng)為spring容器中那些配置@aspectJ切面的bean創(chuàng)建代理,織入切面。
proxy-target-class屬性,默認(rèn)為false,表示使用jdk動(dòng)態(tài)代理織入增強(qiáng),
為true時(shí): 表示使用CGLib動(dòng)態(tài)代理技術(shù)織入增強(qiáng)。
-->
<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"/>-->
<!--
補(bǔ)充:<aop:pointcut>如果位于<aop:aspect>元素中,則命名切點(diǎn)只能被當(dāng)前<aop:aspect>內(nèi)定義的元素訪問(wèn)到,
為了能被整個(gè)<aop:config>元素中定義的所有增強(qiáng)訪問(wèn),則必須在<aop:config>下定義切點(diǎn)。
-->
<aop:config>
<!--切入點(diǎn) execution 表達(dá)式 通過(guò)這個(gè)表達(dá)式篩選連接點(diǎn) -->
<aop:pointcut id="myPointCut" expression="execution(* com.dao.*.*(..))"/>
<aop:aspect ref="myAspectXML">
<!--aop:after 是表示 這個(gè)方法是那種通知類型after 是方法之后
method="after" 這個(gè)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>
測(cè)試
@Test
public void Test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
testDao.save();
/**
* 輸出:
* 前置通知:模擬權(quán)限控制 目標(biāo)對(duì)象:com.dao.TestDaoImpl@704f1591,被增強(qiáng)的方法:save
* 環(huán)繞開始:執(zhí)行目標(biāo)方法前,模擬開啟事務(wù)
* 正在執(zhí)行的方法-->保存
* 環(huán)繞結(jié)束:執(zhí)行目標(biāo)方法后,模擬關(guān)閉事務(wù)
* 后置返回通知:模擬刪除臨時(shí)文件,被增強(qiáng)的方法save
* 最終通知:釋放資源
*/
}
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望能夠您能夠關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Spring MVC多種情況下進(jìn)行文件上傳的實(shí)例
上傳是Web工程中很常見的功能,SpringMVC框架簡(jiǎn)化了文件上傳的代碼,本文給大家總結(jié)了Spring MVC多種情況下進(jìn)行文件上傳的實(shí)例,并通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
SpringBoot 配置文件中配置的中文,程序讀取出來(lái)是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來(lái)是亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題
這篇文章主要介紹了Intellij Idea 多模塊Maven工程中模塊之間無(wú)法相互引用問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringCloud Bus消息總線的實(shí)現(xiàn)
消息總線是一種通信工具,可以在機(jī)器之間互相傳輸消息、文件等,這篇文章主要介紹了SpringCloud Bus消息總線的實(shí)現(xiàn),Spring cloud bus 通過(guò)輕量消息代理連接各個(gè)分布的節(jié)點(diǎn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
詳解java中動(dòng)態(tài)代理實(shí)現(xiàn)機(jī)制
這篇文章主要為大家介紹了java中動(dòng)態(tài)代理實(shí)現(xiàn)機(jī)制的相關(guān)資料,需要的朋友可以參考下2016-01-01
兩分鐘解決IntelliJ IDEA中文亂碼問(wèn)題(推薦)
這篇文章主要介紹了兩分鐘解決IntelliJ IDEA中文亂碼問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
java.lang.Runtime.exec() Payload知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于java.lang.Runtime.exec() Payload知識(shí)點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2020-03-03
Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解
這篇文章主要介紹了Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
java報(bào)錯(cuò):“錯(cuò)誤:編碼GBK?的不可映射字符”解決辦法
當(dāng)Java源代碼中包含中文字符時(shí),我們?cè)谟胘avac編譯時(shí)會(huì)出現(xiàn)“錯(cuò)誤:編碼GBK的不可映射字符”,這篇文章主要給大家介紹了關(guān)于java報(bào)錯(cuò):“錯(cuò)誤:編碼GBK?的不可映射字符”的解決辦法,需要的朋友可以參考下2024-08-08

