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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

xml方式实现aop-快速入门

發布時間:2024/4/13 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xml方式实现aop-快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基于 XML 的 AOP 開發

快速入門

①導入 AOP 相關坐標

②創建目標接口和目標類(內部有切點)

③創建切面類(內部有增強方法)

④將目標類和切面類的對象創建權交給 spring

⑤在 applicationContext.xml 中配置織入關系

⑥測試代碼

?

①導入 AOP 相關坐標

<!--導入spring的context坐標,context依賴aop--> <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version> </dependency> <!-- aspectj的織入 --> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version> </dependency>

②創建目標接口和目標類(內部有切點)

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

③創建切面類(內部有增強方法)

public class MyAspect {//前置增強方法public void before(){System.out.println("前置代碼增強.....");} }

④將目標類和切面類的對象創建權交給 spring

<!--配置目標類--> <bean id="target" class="com.leon.aop.Target"></bean> <!--配置切面類--> <bean id="myAspect" class="com.leon.aop.MyAspect"></bean>

⑤在 applicationContext.xml 中配置織入關系

導入aop命名空間

<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/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

⑤在 applicationContext.xml 中配置織入關系

配置切點表達式和前置增強的織入關系

<aop:config><!--引用myAspect的Bean為切面對象--><aop:aspect ref="myAspect"><!--配置Target的method方法執行時要進行myAspect的before方法前置增強--><aop:before method="before" pointcut="execution(public void com.leon.aop.Target.method())"></aop:before></aop:aspect> </aop:config>

⑥測試代碼

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest {@Autowiredprivate TargetInterface target;@Testpublic void test1(){target.method();} }

?

總結

以上是生活随笔為你收集整理的xml方式实现aop-快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。

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