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

歡迎訪問 生活随笔!

生活随笔

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

javascript

编写第二个Spring程序——AOP实现

發布時間:2025/3/8 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写第二个Spring程序——AOP实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第二個Spring程序 AOP范例

1、新建maven工程
2、在pom.xml文件導入相關jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.6.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.6</version></dependency>
3、在 Packge【service】下創建 【ProductService】類:
package service;public class ProductService {public void doSomeService(){System.out.println("doSomeService");} }
4、在 xml 文件中裝配該 bean:
<bean name="productService" class="service.ProductService" />
5、在【TestSpring】中編寫測試代碼,運行:
package test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pojo.Source; import service.ProductService;public class TestSpring {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});ProductService productService = (ProductService) context.getBean("productService");productService.doSomeService();} }

運行結果

doSomeService
6、在 Packge【aspect】下準備日志切面 【LoggerAspect】類:
package aspect;import org.aspectj.lang.ProceedingJoinPoint;public class LoggerAspect {public Object log(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("start log:" + joinPoint.getSignature().getName());Object object = joinPoint.proceed();System.out.println("end log:" + joinPoint.getSignature().getName());return object;} }
7、在 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:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean name="productService" class="service.ProductService" /><bean id="loggerAspect" class="aspect.LoggerAspect"/><!-- 配置AOP --><aop:config><!-- where:在哪些地方(包.類.方法)做增加 --><aop:pointcut id="loggerCutpoint"expression="execution(* service.ProductService.*(..)) "/><!-- what:做什么增強 --><aop:aspect id="logAspect" ref="loggerAspect"><!-- when:在什么時機(方法前/后/前后) --><aop:around pointcut-ref="loggerCutpoint" method="log"/></aop:aspect></aop:config> </beans>
8、再次運行 TestSpring 中的測試代碼,代碼并沒有改變,但是在業務方法運行之前和運行之后,都分別輸出了日志信息:
start log:doSomeService doSomeService end log:doSomeService

總結

以上是生活随笔為你收集整理的编写第二个Spring程序——AOP实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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