Spring Aop注解實現(xiàn)
更新時間:2021年07月16日 14:33:44 作者:寧在春
本文我們通過Spring AOP和Java的自定義注解來實現(xiàn)日志的插入功能,非常不錯,具有一定的參考借鑒價值,需要的朋友一起看看吧,希望對你有所幫助
Spring-aop-理論知識 Spring-Aop-注解實現(xiàn) 項目結(jié)構(gòu)圖

具體步驟:
1、創(chuàng)建maven 項目 導(dǎo)入依賴 創(chuà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、寫一個接口 及 其實現(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ù)獲得目標對象的信息
* @Before("myPointCut()") ==
* <aop:after method="after" pointcut-ref="myPointCut"/>
**/
@Before("myPointCut()")
public void before(JoinPoint jp){
System.out.print("前置通知:模擬權(quán)限控制");
System.out.println("目標對象:"+jp.getTarget()+",被增強的方法:"+jp.getSignature().getName());
}
@AfterReturning("myPointCut()")
public void afterReturning(JoinPoint jp){
System.out.print("后置返回通知:"+"模擬刪除臨時文件");
System.out.println(",被增強的方法"+jp.getSignature().getName());
}
@Around("myPointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("環(huán)繞開始:執(zhí)行目標方法前,模擬開啟事務(wù)");
Object obj = pjp.proceed();
System.out.println("環(huán)繞結(jié)束:執(zhí)行目標方法后,模擬關(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"/>
<!--啟動基于注解的AspectJ支持-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
測試
@Test
public void Test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
testDao.delete();
/**
* 輸出:
* 環(huán)繞開始:執(zhí)行目標方法前,模擬開啟事務(wù)
* 前置通知:模擬權(quán)限控制目標對象:com.dao.TestDaoImpl@2bef51f2,被增強的方法:delete
* 正在執(zhí)行的方法-->刪除
* 后置返回通知:模擬刪除臨時文件,被增強的方法delete
* 最終通知:釋放資源
* 環(huán)繞結(jié)束:執(zhí)行目標方法后,模擬關(guān)閉事務(wù)
*/
}
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java中常用解析工具jackson及fastjson的使用
今天給大家?guī)淼氖顷P(guān)于Java解析工具的相關(guān)知識,文章圍繞著jackson及fastjson的使用展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring?Boot?Admin?監(jiān)控指標接入Grafana可視化的實例詳解
Spring Boot Admin2 自帶有部分監(jiān)控圖表,如圖,有線程、內(nèi)存Heap和內(nèi)存Non Heap,這篇文章主要介紹了Spring?Boot?Admin?監(jiān)控指標接入Grafana可視化,需要的朋友可以參考下2022-11-11
Java 基礎(chǔ):string中的compareTo方法
這篇文章主要介紹了Java 基礎(chǔ):string中的compareTo方法,文章圍繞string中的compareTo方法的相關(guān)資料展開文章詳細內(nèi)容,希望對待大家有所幫助2021-12-12
Java多線程中Thread.currentThread()和this的區(qū)別詳解
這篇文章主要介紹了Java多線程中Thread.currentThread()和this的區(qū)別詳解,Thread.currentThread()方法返回的是對當前正在執(zhí)行的線程對象的引用,this代表的是當前調(diào)用它所在函數(shù)所屬的對象的引用,需要的朋友可以參考下2023-08-08
Spring boot JPA實現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例
這篇文章主要介紹了Spring boot JPA實現(xiàn)分頁和枚舉轉(zhuǎn)換代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

