日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-Aop-注解实现

發布時間:2025/3/19 javascript 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-Aop-注解实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring-Aop-注解實現

    • Spring-aop-理論知識
    • Spring-Aop-注解實現 項目結構圖
    • 具體步驟:
      • 1、創建maven 項目 導入依賴 創建好項目結構
      • 2、寫一個接口 及 其實現類
      • 3、切面類
      • 4、application.xml 文件
    • 測試
    • 自言自語

Spring-aop-理論知識

Spring-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></dependencies>

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

/*** @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("正在執行的方法-->刪除");} }

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 接口作為參數獲得目標對象的信息* @Before("myPointCut()") ==* <aop:after method="after" pointcut-ref="myPointCut"/>**/@Before("myPointCut()")public void before(JoinPoint jp){System.out.print("前置通知:模擬權限控制");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("環繞開始:執行目標方法前,模擬開啟事務");Object obj = pjp.proceed();System.out.println("環繞結束:執行目標方法后,模擬關閉事務");return obj;}@AfterThrowing(value = "myPointCut()",throwing = "throwable")public void except(Throwable throwable){System.out.println("異常通知:"+"程序執行異常"+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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://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>

測試

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

自言自語

完成一小段任務拉。輕松一會。
干別的活去。

總結

以上是生活随笔為你收集整理的Spring-Aop-注解实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。