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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

spring-基于xml的aop开发-快速入门

發布時間:2024/10/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-基于xml的aop开发-快速入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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>

如果使用spring原生的aop進行織入,則導入spring-context就夠了(因為spirng-context包含spring-aop),但是spring官方推薦使用第三方小框架aspectjweaver進行織入(spring不排斥任何優秀的框架)


2.創建目標接口和目標類

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

3.創建切面類

public class MyAspect {public void before(){System.out.println("前置增強。。。");}}

4.將目標和切面類的對象創建權交給spring
注意:這一步只是把目標對象類和切面對象類交給spring管理,現在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中配置織入關系
aop:aspect標簽配置切面,那切面是誰呢,使用ref引入切面的唯一標識id,此時這一步配置之后,第二個bean標簽引入的類就真正成為了切面類
aop:before:通知的類型,method引入切面中的增強方法;ponintcut:切點表達式(最后講)

<?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:aspect></aop:config> </beans>

6.測試代碼

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest {@Autowired@Qualifier("target")private TargetInterface target;@Testpublic void test1(){target.save();} }

結果:


execution
public:修飾符(可以省略)
void 返回值類型
com.hao.aop:包名
Target:類名
save(參數):方法名

execution(void com.hao.aop.Target.*(…)) :表示Target類下所有無返回值的方法都會被增強
execution( * com.hao.aop.. (. .):表示任意類的任意方法都可以
execution(* com.hao.aop. . *. *(. .):表示aop包下的任意類方法或者任意子包下的任意類方法

總結

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

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