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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring-xml实现aop-通知的种类

發(fā)布時間:2024/10/5 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-xml实现aop-通知的种类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如果本代碼有疑問,請訪問spring-aop快速入門或者spring-aop動態(tài)代理技術(底層分析)

1.導入aop的相關坐標

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.5.RELEASE</version></dependency>

2.創(chuàng)建目標接口和目標類

public interface TargetInterface {public void save(); } public class Target implements TargetInterface{public void save() {System.out.println("save running....");} }

3.創(chuàng)建切面類

public class MyAspect {public void before(){System.out.println("前置增強...");}public void afterReturning(){System.out.println("后置增強...");}//ProceedingJoinPoint://切入點public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println("環(huán)繞前增強...");//切點方法,spring封裝好的Object proceed = point.proceed();System.out.println("環(huán)繞后增強...");return proceed;} }

4.將目標和切面類的對象創(chuàng)建權交給spring

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> </beans>

5.在applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> <!-- 配置織入:告訴spring框架哪些方法需要進行哪些增強(前置增強、后置增強)--><aop:config> <!-- 聲明切面 切面=切點+通知--><aop:aspect ref="aspect"><aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before><aop:after method="afterReturning" pointcut="execution(public void com.hao.aop.Target.save())"></aop:after><aop:around method="around" pointcut="execution(public void com.hao.aop.Target.save())"></aop:around></aop:aspect></aop:config> </beans>

結果:


切點表達式的抽取,為了方便以后更改

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> <!-- 配置織入:告訴spring框架哪些方法需要進行哪些增強(前置增強、后置增強)--><aop:config> <!-- 聲明切面 切面=切點+通知--><aop:aspect ref="aspect"> <!-- 抽取切入點表達式--><aop:pointcut id="myPointcut" expression="execution(public void com.hao.aop.Target.save())"></aop:pointcut><aop:before method="before" pointcut-ref="myPointcut"></aop:before><aop:after method="afterReturning" pointcut-ref="myPointcut"></aop:after><aop:around method="around" pointcut-ref="myPointcut"></aop:around></aop:aspect></aop:config> </beans>

總結

以上是生活随笔為你收集整理的spring-xml实现aop-通知的种类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。