Spirng使用Aspectj实现AOP
Aspectj實(shí)現(xiàn)AOP有兩種方式:
(1)基于aspectj的xml配置;
(2)基于aspectj的注解方式;
?
一、基于aspectj的xml配置:
1、導(dǎo)入相關(guān)的AOP的jar包:
2、創(chuàng)建Spring核心配置文件,導(dǎo)入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、使用表達(dá)式配置切入點(diǎn):
(1)表達(dá)式格式:
execution(<訪問(wèn)修飾符>?<返回類(lèi)型><方法名>(<參數(shù)>)<異常>)
(2)常用的表達(dá)式舉例:
舉例:
①execution(* com.zwp.aop.User.add(..))
②execution(* com.zwp.aop.User.*(..))
③execution(* *.*(..))
④匹配所有save開(kāi)頭的方法:execution(* save*(..))
第一個(gè)*:表示所有的修飾類(lèi)型。
(3)代碼測(cè)試:
//原始方法: public class MainTest {public void text1(){System.out.println("主方法....");} } //增強(qiáng)的方法: public class SecondText {public void before1(){System.out.println("前置增強(qiáng)");}public void after1(){System.out.println("后置增強(qiáng)");}//環(huán)繞增強(qiáng)public void round1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{//方法之前:System.out.println("方法之前...");//執(zhí)行方法: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、創(chuàng)建兩個(gè)類(lèi)的對(duì)象 --> <bean id="mainTest" class="com.zwp.aop.MainTest"></bean><bean id="secondText" class="com.zwp.aop.SecondText"></bean><!-- 2、配置aop操作 --><aop:config><!-- 2.1配置切點(diǎn) --><aop:pointcut expression="execution(* com.zwp.aop.MainTest.*(..))" id="pointcut1"/><!-- 2.2配置切面:即把增強(qiáng)用到方法上面的過(guò)程 --><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>測(cè)試類(lèi):
public class Test2 {@Testpublic void test4(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");MainTest mainTest = (MainTest) context.getBean("mainTest");mainTest.text1();} }運(yùn)行結(jié)果:
?
二、基于aspectj的注解方式:
(1)導(dǎo)入與AOP相關(guān)的jar包:
(2)創(chuàng)建對(duì)象:
(3)開(kāi)啟Aop操作:
(4)在增強(qiáng)類(lèi)使用注解@Aspect,并在方法上使用注解完成增強(qiáng)配置。
測(cè)試代碼:
//目標(biāo)對(duì)象Target public class Annoaop {public void text1(){System.out.println("基于aspecj的注解aop操作的主方法....");} }在spring的applicationContext.xml文件配置中,創(chuàng)建對(duì)象與開(kāi)啟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.開(kāi)啟aop操作 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 2.創(chuàng)建對(duì)象 --><bean id="anno" class="com.zwp.annoAop.Annoaop"></bean><bean id="add" class="com.zwp.annoAop.Add"></bean><!-- 基于aspecj的注解aop操作 end --> </beans> //類(lèi)說(shuō)明:增強(qiáng)類(lèi) //3.1在增強(qiáng)類(lèi)上面使用注解 @Aspect public class Add {//3.2 在方法上使用注解完成增強(qiáng)配置:@Before(value="execution(* com.zwp.annoAop.Annoaop.*(..))")public void before1(){System.out.println("前置增強(qiáng)...");} } public class Test2 {@Testpublic void test5(){ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");Annoaop annoaop = (Annoaop) context.getBean("anno");annoaop.text1();} }運(yùn)行結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的Spirng使用Aspectj实现AOP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring的Bean实例化、属性注入、
- 下一篇: spring配置c3p0连接池、spri