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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring-基于注解的aop开发(快速入门)

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

步驟:
1.導(dǎo)入坐標(biāo)

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.4</version></dependency>

2.創(chuàng)建目標(biāo)類和接口(內(nèi)部有切點)

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

3.創(chuàng)建切面類(內(nèi)部有增強(qiáng)方法)

@Component @Aspect //標(biāo)注當(dāng)前aspect是切面類 public class MyAspect {//配置織入關(guān)系 value=切點表達(dá)式@Before(value = "execution(public void com.hao.anno.Target.save())")public void before(){System.out.println("前置增強(qiáng)...");}public void afterReturning(){System.out.println("后置增強(qiáng)...");}public Object around(ProceedingJoinPoint point) throws Throwable { //切入點System.out.println("環(huán)繞前增強(qiáng)...");//切點方法Object proceed = point.proceed();System.out.println("環(huán)繞后增強(qiáng)...");return proceed;} }

4.將目標(biāo)類和切面類的對象創(chuàng)建權(quán)交給spring
5.在切面類中使用注解配置織入關(guān)系

第三步和第二步已經(jīng)實現(xiàn)
6.在配置文件中開啟組件掃描和aop自動代理

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 組件掃描--><context:component-scan base-package="com.hao.anno"></context:component-scan><!-- aop自動代理--><aop:aspectj-autoproxy/> </beans>

7.測試

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContextanno.xml") public class AnnoTest {@Autowiredprivate TargetInterface target;@Testpublic void test2(){target.save();}}

結(jié)果:
前置增強(qiáng)…
save running…

總結(jié)

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

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