Spring AOP示例 一.使用Annotation 1.定義切面 package com.yyj.aspect;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;
@Component @Aspect public class TestAspect { @Pointcut("execution(* com.yyj.service.*.*(..))") public void testPointCut(){ } } 注: @Aspect說明該類是個(gè)切面,@Component將該bean自動(dòng)注入到springIOC容器, 默認(rèn)id為類名第一個(gè)字母轉(zhuǎn)小寫; @Pointcut("execution(* com.yyj.service.*.*(..))")定義一個(gè)切入點(diǎn),, 后面的表達(dá)式說明對(duì)com.yyj.service包及子包下的所有方法進(jìn)行攔截,植入其它操作,, 其它操作由對(duì)應(yīng)的Advise定義,。 2.定義advise package com.yyj.aspect;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
@Aspect @Component public class TestAdvise { @Before("com.yyj.aspect.TestAspect.testPointCut()") private void testBefore(){ System.out.println("begin-----in-----"); System.out.println("before test..."); System.out.println("end -----in-----"); } } 注: @Before("com.yyj.aspect.TestAspect.testPointCut()"),定義了testPointCut這個(gè)切入點(diǎn) 要做的操作是在執(zhí)行被切入方法之前先執(zhí)行testBefore方法,。 3.配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:aop="http://www./schema/aop" xmlns:context="http://www./schema/context" xmlns:jee="http://www./schema/jee" xmlns:lang="http://www./schema/lang" xmlns:tx="http://www./schema/tx" xmlns:util="http://www./schema/util" xsi:schemaLocation="http://www./schema/beanshttp://www./schema/beans/spring-beans-3.0.xsd http://www./schema/aophttp://www./schema/aop/spring-aop-3.0.xsd http://www./schema/contexthttp://www./schema/context/spring-context-3.0.xsd http://www./schema/jeehttp://www./schema/jee/spring-jee-3.0.xsd http://www./schema/langhttp://www./schema/lang/spring-lang-3.0.xsd http://www./schema/txhttp://www./schema/tx/spring-tx-3.0.xsd http://www./schema/utilhttp://www./schema/util/spring-util-3.0.xsd"> <aop:aspectj-autoproxy/> <!-- 啟動(dòng)Spring注解自動(dòng)注入Bean --> <context:component-scan base-package="com.yyj" /> </beans> 4.定義測(cè)試類 package com.yyj.service; import org.springframework.stereotype.Component;
@Component public class Testa{ public void test(){ System.out.println("test..."); } }
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yyj.service.Testa;
public class MainTest {
private static ClassPathXmlApplicationContext springCtx;
synchronized static void startup() { if (springCtx == null) { System.out.println("start up..."); springCtx = new ClassPathXmlApplicationContext(); springCtx .setConfigLocations(new String[] { "applicationContext.xml" }); springCtx.refresh(); Testa ta = (Testa)springCtx.getBean("testa"); ta.test(); System.out.println("start up...ok"); } }
public static void main(String[] args) { new MainTest().startup(); } }
二.不使用Annotation注解也可以如下類似配置 <aop:aspectj-autoproxy/> <!-- 啟動(dòng)Spring注解 --> <context:component-scan base-package="com.yyj" /> <!--<tx:annotation-driven proxy-target-class="true" /> --> <aop:config> <aop:aspect ref="myPoint"> <aop:pointcut expression="execution(* com.yyj.service.FooService.getFoo(..))" id="pointCut"/> <aop:before pointcut-ref="pointCut" method="myAroundMethod"/> </aop:aspect> </aop:config> 注:PointCut和Advise可以在一個(gè)類中,,也可以分開定義
工程用到j(luò)ar
|