<context:component-scan base-package="com.zztaiwll.service,com.zztaiwll.util.annotion"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan> 4,本工程使用的maven,所以引入兩個相關的包,分別是aspectjweaver.jar和aspectjrt.jar <spring.version>4.0.2.RELEASE</spring.version> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> </dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency>5,定義切入點(1)所謂的定義切入點,實質就是為一個切入點表達式起一個名稱,從而容許在多個增強處理中重用該名稱(2)切入點定義包含兩個部分: [1]一個切入點表達式 [2]一個包含名字和任意參數的方法簽名例如:@Aspectpublic class demo {@Pointcut("execution(* com.zztaiwll.controller.*.*(..))")public void myPointcut(){};}@Aspectpublic class demoDesc {//直接使用demo切面類的myPointcut切入點 @AfterReturning(returning="rtc",pointcut="demo.myPointcut()") //聲明 rtc時指定類型會相知目標方法必須返回指定類型的值或者您沒有返回值 public void log(Object rtc){ System.out.println("獲取該函數返回至"+rvt); }}6,切入點指示符Spring AOP一共支持如下幾種切入點指示符【1】,execution:用于匹配方法的連接點,是springaop的最主要的切入點指示符execution(modifiters-pattern?ret-type-pattern declaring-type-pattern ?name-pattern(param-pattern) throw-pattern)modifiters-pattern:指定方法的修飾符,支持通配符,該部分可以省略ret-type-pattern:指定方法的返回值類型,支持通配符,可以使用*通配符來匹配所有返回值類型declaring-type-pattern:指定方法所屬的類,支持通配符,可省略name-pattern:指定匹配的方法名,支持通配符,可使用*指定所有方法param-pattern:指定方法中的形參列表,支持兩個通配符,即“*”和“..",其中*代表一個人以參數,..代表另個或者多個任意類型的參數throw-pattern:制定方法聲明拋出的異常,支持通配符,可省略如execution(* com.zztaiwll.controller.*.*(..))指的是controller下所有的類中的所有方法【2】,within:用于限定匹配特定類型的連接點,當使用Springaop的時候,智能匹配方法執行的連接點如witnin(com.zztaiwll.controller..*)【3】,this用于限定AOP代理必須是指定類型的實例,匹配該對象的所有連接點。當使用Spring AOP的時候,只能匹配方法的連接點this(com.zztaiwll.controller.UserController)【4】,target:用于限定目標對象必須是指定的類型的實例,匹配該對象所有的連接點,當使用Spring AOP的時候,只能匹配方法的連接點target(com.zztaiwll.controller.UserController)【5】,args:用于對連接點的類型進行限制,要求參數類型是指定類型的實例。當使用Spring AOP的時候,只能匹配方法的連接點args(java.io.Serializable)【6】,bean:用于限定之匹配指定Bean十里內的連接點,實際上只是用方法執行作為連接點bean表達式是需要傳入Bean的id或name,表示只匹配該bean實例內的連接點,支持使用*通配符bean(*service)7,織入點表達式spring支持使用如下三個邏輯運算符來組合切入點表達式【1】&&:要求連接點同時匹配兩個切入點表達式【2】||:滿足任意一個即可【3】!要求連接點不匹配指定的切入點表達式如 @Around(value="execution(* com.zztaiwll.controller.*.*(..))&& @annotation(json)")8,編寫相應的方法{1}Before,After,AfterThrowing,AfterReturning@Component//聲明這是一個切面Bean@Aspectpublic class ServiceAspect { private final static Log log = LogFactory.getLog(ServiceAspect.class); //配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點 @Before("execution(* com.zztaiwll.controller.*.*(..))") public void aspect(JoinPoint jp) throws Throwable{ Object []obj=jp.getArgs();//獲取參數 System.out.println("1111111111111111111111"); } //(1),pointcut/value:這兩個屬性是一樣的,他們都是用于指定該切入點對應的切入表達式 //(2),returning:該屬性制定一個形參名,用于表示Advice方法中可定義與此同名的形參,該形參用于訪問目標方法的返回值,他可以用于限定切入點之匹配具體對應返回值的方法 @AfterReturning(returning="rvt", pointcut="execution(* com.zztaiwll.controller.*.*(..))") public Object after(Object rvt){ System.out.println("獲取該函數返回至"+rvt); return rvt; } //(1),pointcut/value:這兩個屬性是一樣的,他們都是用于指定該切入點對應的切入表達式 //(2),throwing:該屬性制定一個形參名,用于表示Advice方法中可定義與此同名的形參,該形參可用于訪問目標方法拋出的異常 @AfterThrowing(throwing="ex",pointcut="execution(* com.zztaiwll.controller.*.*(..))") public void throwError(Throwable ex){ System.out.println("目標方法拋出異常"+ex); System.out.println("模擬Advice對異常的修復"); } //AfterReturning和After的區別 //(1),AfterReturning增強處理只有在目標方法完全成功后才被織入 //(2),After增強處理不管目標方法如何結束,他都會被織入 @After("execution(* com.zztaiwll.controller.*.*(..))") public void afterS(JoinPoint jp){ Object []obj=jp.getArgs();//獲取參數 System.out.println("模擬方法釋放資源"); }}{2},Around@Around注解用于修飾Around增強處理,它的功能比較強大,他近似等于Before增強處理和AfterReturn增強處理的總和,Around增強處理即可以在執行目標方法之前織入增強動作也可在執行目標方法之后織入增強動作//聲明這是一個組件@Component//聲明這是一個切面Bean@Aspectpublic class ServiceAspect { private final static Log log = LogFactory.getLog(ServiceAspect.class); //配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點 @Around(value="execution(* com.zztaiwll.controller.*.*(..))") public Object aspect(ProceedingJoinPoint pjd, json json) throws Throwable{ Object[] args=pjd.getArgs();//獲取參數 Object rvt=pjd.proceed();//獲取返回結果 return rvt; }}第二部分,做自定義注解,注意關鍵一點要在spring-servlet.xml中配上<context:component-scan base-package="com.zztaiwll.service,com.zztaiwll.util.annotion"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan>【1】,配置自定義注解import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)//@Target({ ElementType.METHOD })//public @interface json {}【2】aop實現注解//聲明這是一個組件@Component//聲明這是一個切面Bean@Aspectpublic class ServiceAspect { private final static Log log = LogFactory.getLog(ServiceAspect.class); //配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點 @Around(value="execution(* com.zztaiwll.controller.*.*(..))&& @annotation(json)") public Object aspect(ProceedingJoinPoint pjd, json json) throws Throwable{ Object[] args=pjd.getArgs(); Object rvt=pjd.proceed(); Object objs=StringConvertJSON.toJSON(rvt); return objs; }}
新聞熱點
疑難解答