Spring Aop注解實(shí)現(xiàn)
Spring-aop-理論知識(shí) Spring-Aop-注解實(shí)現(xiàn) 項(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>
</dependencies>
2、寫一個(gè)接口 及 其實(shí)現(xiàn)類
/**
* @version 1.0
* @author: crush
* @date: 2021-03-05 10:26
*/
public interface TestDao {
public void delete();
}
/**
* @version 1.0
* @author: crush
* @date: 2021-03-05 10:27
*/
@Service
public class TestDaoImpl implements TestDao {
public void delete() {
System.out.println("正在執(zhí)行的方法-->刪除");
}
}
3、切面類
/**
* @version 1.0
* @author: crush
* @date: 2021-03-10 18:04
*/
@Aspect
@Component
public class MyAspectAnnotation {
@Pointcut("execution(* com.dao.*.*(..))")
private void myPointCut(){
}
/**
* 前置通知 使用JoinPoint 接口作為參數(shù)獲得目標(biāo)對(duì)象的信息
* @Before("myPointCut()") ==
* <aop:after method="after" pointcut-ref="myPointCut"/>
**/
@Before("myPointCut()")
public void before(JoinPoint jp){
System.out.print("前置通知:模擬權(quán)限控制");
System.out.println("目標(biāo)對(duì)象:"+jp.getTarget()+",被增強(qiáng)的方法:"+jp.getSignature().getName());
}
@AfterReturning("myPointCut()")
public void afterReturning(JoinPoint jp){
System.out.print("后置返回通知:"+"模擬刪除臨時(shí)文件");
System.out.println(",被增強(qiáng)的方法"+jp.getSignature().getName());
}
@Around("myPointCut()")
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;
}
@AfterThrowing(value = "myPointCut()",throwing = "throwable")
public void except(Throwable throwable){
System.out.println("異常通知:"+"程序執(zhí)行異常"+throwable.getMessage());
}
@After("myPointCut()")
public void after(){
System.out.println("最終通知:釋放資源");
}
}
4、application.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.dao"/>
<context:component-scan base-package="com.aspect"/>
<!--啟動(dòng)基于注解的AspectJ支持-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
測(cè)試
@Test
public void Test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
testDao.delete();
/**
* 輸出:
* 環(huán)繞開始:執(zhí)行目標(biāo)方法前,模擬開啟事務(wù)
* 前置通知:模擬權(quán)限控制目標(biāo)對(duì)象:com.dao.TestDaoImpl@2bef51f2,被增強(qiáng)的方法:delete
* 正在執(zhí)行的方法-->刪除
* 后置返回通知:模擬刪除臨時(shí)文件,被增強(qiáng)的方法delete
* 最終通知:釋放資源
* 環(huán)繞結(jié)束:執(zhí)行目標(biāo)方法后,模擬關(guān)閉事務(wù)
*/
}
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java中常用解析工具jackson及fastjson的使用
今天給大家?guī)?lái)的是關(guān)于Java解析工具的相關(guān)知識(shí),文章圍繞著jackson及fastjson的使用展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
SpringCloud Ribbon負(fù)載均衡實(shí)例解析
這篇文章主要介紹了SpringCloud Ribbon負(fù)載均衡實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring?Boot?Admin?監(jiān)控指標(biāo)接入Grafana可視化的實(shí)例詳解
Spring Boot Admin2 自帶有部分監(jiān)控圖表,如圖,有線程、內(nèi)存Heap和內(nèi)存Non Heap,這篇文章主要介紹了Spring?Boot?Admin?監(jiān)控指標(biāo)接入Grafana可視化,需要的朋友可以參考下2022-11-11
Java 基礎(chǔ):string中的compareTo方法
這篇文章主要介紹了Java 基礎(chǔ):string中的compareTo方法,文章圍繞string中的compareTo方法的相關(guān)資料展開文章詳細(xì)內(nèi)容,希望對(duì)待大家有所幫助2021-12-12
Java多線程中Thread.currentThread()和this的區(qū)別詳解
這篇文章主要介紹了Java多線程中Thread.currentThread()和this的區(qū)別詳解,Thread.currentThread()方法返回的是對(duì)當(dāng)前正在執(zhí)行的線程對(duì)象的引用,this代表的是當(dāng)前調(diào)用它所在函數(shù)所屬的對(duì)象的引用,需要的朋友可以參考下2023-08-08
Java 獲取當(dāng)前設(shè)備的 IP 地址(最新推薦)
Internet 協(xié)議 (IP) 地址可以是連接到 TCP/IP 網(wǎng)絡(luò)的每個(gè)設(shè)備的標(biāo)識(shí)符,該標(biāo)識(shí)符用于識(shí)別和定位中間通信的節(jié)點(diǎn),這篇文章主要介紹了在 Java 中獲取當(dāng)前設(shè)備的 IP 地址,需要的朋友可以參考下2023-06-06
Spring boot JPA實(shí)現(xiàn)分頁(yè)和枚舉轉(zhuǎn)換代碼示例
這篇文章主要介紹了Spring boot JPA實(shí)現(xiàn)分頁(yè)和枚舉轉(zhuǎn)換代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
JavaWeb中的常用的請(qǐng)求傳參注解說(shuō)明
這篇文章主要介紹了JavaWeb中的常用的請(qǐng)求傳參注解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

