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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring基于注解的AOP配置

發布時間:2025/4/16 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring基于注解的AOP配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dym</groupId><artifactId>day03_eesy_05annotationAOP</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency></dependencies></project>

IAccountService.java

package com.itheima.service;/*** 賬戶的業務層接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();/*** 模擬更新賬戶* @param i*/void updateAccount(int i);/*** 刪除賬戶* @return*/int deleteAccount(); }

AccountServiceImpl.java

package com.itheima.service.impl;import com.itheima.service.IAccountService; import org.springframework.stereotype.Service;/*** 賬戶的業務層實現類*/ @Service("accountService") public class AccountServiceImpl implements IAccountService{@Overridepublic void saveAccount() {System.out.println("執行了保存"); // int i=1/0;}@Overridepublic void updateAccount(int i) {System.out.println("執行了更新"+i);}@Overridepublic int deleteAccount() {System.out.println("執行了刪除");return 0;} }

Logger.java

package com.itheima.utils;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;/*** 用于記錄日志的工具類,它里面提供了公共的代碼*/ @Component("logger") @Aspect//表示當前類是一個切面類 public class Logger {@Pointcut("execution(* com.itheima.service.impl.*.*(..))")private void pt1(){}/*** 前置通知*/ // @Before("pt1()")public void beforePrintLog(){System.out.println("前置通知Logger類中的beforePrintLog方法開始記錄日志了。。。");}/*** 后置通知*/ // @AfterReturning("pt1()")public void afterReturningPrintLog(){System.out.println("后置通知Logger類中的afterReturningPrintLog方法開始記錄日志了。。。");}/*** 異常通知*/ // @AfterThrowing("pt1()")public void afterThrowingPrintLog(){System.out.println("異常通知Logger類中的afterThrowingPrintLog方法開始記錄日志了。。。");}/*** 最終通知*/ // @After("pt1()")public void afterPrintLog(){System.out.println("最終通知Logger類中的afterPrintLog方法開始記錄日志了。。。");}@Around("pt1()")public Object aroundPringLog(ProceedingJoinPoint pjp){Object rtValue = null;try{Object[] args = pjp.getArgs();//得到方法執行所需的參數System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。前置");rtValue = pjp.proceed(args);//明確調用業務層方法(切入點方法)System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。后置");return rtValue;}catch (Throwable t){System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。異常");throw new RuntimeException(t);}finally {System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。最終");}} }

bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置spring創建容器時要掃描的包--><context:component-scan base-package="com.itheima"></context:component-scan><!-- 配置spring開啟注解AOP的支持 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

AOPTest.java

package com.itheima.test;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 測試AOP的配置*/ public class AOPTest {public static void main(String[] args) {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.獲取對象IAccountService as = (IAccountService)ac.getBean("accountService");//3.執行方法as.saveAccount();} }

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

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

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