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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Spirng使用Aspectj实现AOP

發布時間:2024/9/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spirng使用Aspectj实现AOP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Aspectj實現AOP有兩種方式:

(1)基于aspectj的xml配置;

(2)基于aspectj的注解方式;

?

一、基于aspectj的xml配置:

1、導入相關的AOP的jar包:

2、創建Spring核心配置文件,導入aop的約束

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

3、使用表達式配置切入點:

(1)表達式格式:

execution(<訪問修飾符>?<返回類型><方法名>(<參數>)<異常>)

(2)常用的表達式舉例:

舉例:

①execution(* com.zwp.aop.User.add(..))

②execution(* com.zwp.aop.User.*(..))

③execution(* *.*(..))

④匹配所有save開頭的方法:execution(* save*(..))

第一個*:表示所有的修飾類型。

(3)代碼測試:

//原始方法: public class MainTest {public void text1(){System.out.println("主方法....");} } //增強的方法: public class SecondText {public void before1(){System.out.println("前置增強");}public void after1(){System.out.println("后置增強");}//環繞增強public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{//方法之前:System.out.println("方法之前...");//執行方法:proceedingJoinPoint.proceed();//方法之后:System.out.println("方法之后...");} }

spring的applicationContext.xml配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 使用aop操作 start--><!-- 1、創建兩個類的對象 --> <bean id="mainTest" class="com.zwp.aop.MainTest"></bean><bean id="secondText" class="com.zwp.aop.SecondText"></bean><!-- 2、配置aop操作 --><aop:config><!-- 2.1配置切點 --><aop:pointcut expression="execution(* com.zwp.aop.MainTest.*(..))" id="pointcut1"/><!-- 2.2配置切面:即把增強用到方法上面的過程 --><aop:aspect ref="secondText"><aop:before method="before1" pointcut-ref="pointcut1"/><aop:after method="after1" pointcut-ref="pointcut1"/><aop:around method="round1" pointcut-ref="pointcut1"/></aop:aspect></aop:config><!-- 使用aop操作 end--> </beans>

測試類:

public class Test2 {@Testpublic void test4(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");MainTest mainTest = (MainTest) context.getBean("mainTest");mainTest.text1();} }

運行結果:

?

二、基于aspectj的注解方式:

(1)導入與AOP相關的jar包:

(2)創建對象:

(3)開啟Aop操作:

(4)在增強類使用注解@Aspect,并在方法上使用注解完成增強配置。

測試代碼:

//目標對象Target public class Annoaop {public void text1(){System.out.println("基于aspecj的注解aop操作的主方法....");} }

在spring的applicationContext.xml文件配置中,創建對象與開啟AOP操作:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 基于aspecj的注解aop操作 start --><!-- 1.開啟aop操作 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 2.創建對象 --><bean id="anno" class="com.zwp.annoAop.Annoaop"></bean><bean id="add" class="com.zwp.annoAop.Add"></bean><!-- 基于aspecj的注解aop操作 end --> </beans> //類說明:增強類 //3.1在增強類上面使用注解 @Aspect public class Add {//3.2 在方法上使用注解完成增強配置:@Before(value="execution(* com.zwp.annoAop.Annoaop.*(..))")public void before1(){System.out.println("前置增強...");} } public class Test2 {@Testpublic void test5(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");Annoaop annoaop = (Annoaop) context.getBean("anno");annoaop.text1();} }

運行結果:

?

總結

以上是生活随笔為你收集整理的Spirng使用Aspectj实现AOP的全部內容,希望文章能夠幫你解決所遇到的問題。

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