生活随笔
收集整理的這篇文章主要介紹了
Spring框架笔记(二十二)——切点表达式的重用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
在編寫 AspectJ 切面時, 可以直接在通知注解中書寫切入點表達式. 但同一個切點表達式可能會在多個通知中重復出現.
在 AspectJ 切面中, 可以通過 @Pointcut 注解將一個切入點聲明成簡單的方法. 切入點的方法體通常是空的, 因為將切入點定義與應用程序邏輯混在一起是不合理的.?
切入點方法的訪問控制符同時也控制著這個切入點的可見性. 如果切入點要在多個切面中共用, 最好將它們集中在一個公共的類中. 在這種情況下, 它們必須被聲明為 public. 在引入這個切入點時, 必須將類名也包括在內. 如果類沒有與這個切面放在同一個包中, 還必須包含包名.
其他通知可以通過方法名稱引入該切入點.
好吧,我們之前提過連接點、切點的定義。現在還能分清嗎?如果這里需要做個分辨,那么你可以將切點理解為切點表達式,連接點為各個通知的JointPoint參數。
我們看兩個切面的定義,其中一個切面的類方法中有一個方法用于聲明切點表達式,這樣該切點類的其他通知都可以使用這個方法來注解各個通知的切點表達式;其他切點的通知也可以只用,但要寫清類名。
package?com.happBKs.spring.aopbasic.aop1;import?java.util.Arrays;
import?java.util.List;import?org.aspectj.lang.JoinPoint;
import?org.aspectj.lang.ProceedingJoinPoint;
import?org.aspectj.lang.annotation.After;
import?org.aspectj.lang.annotation.AfterReturning;
import?org.aspectj.lang.annotation.AfterThrowing;
import?org.aspectj.lang.annotation.Around;
import?org.aspectj.lang.annotation.Aspect;
import?org.aspectj.lang.annotation.Before;
import?org.aspectj.lang.annotation.Pointcut;
import?org.springframework.core.annotation.Order;
import?org.springframework.stereotype.Component;@Order(Integer.MAX_VALUE)
@Aspect
@Component
public?class?DataValidateAspect?{/**?定義一個方法用于聲明切點表達式。一般的,該方法中再也不需要添加其他代碼*?使用@Pointcut注解聲明切點表達式*/@Pointcut("execution(public?int?com.happBKs.spring.aopbasic.aop1.AtithmeticCalculate.*(..))")public?void?declarePointCut(){}@Before("declarePointCut()")public?boolean?beforeMethod(JoinPoint?joinPoint){String?methodName=joinPoint.getSignature().getName();List<Object>?args=Arrays.asList(joinPoint.getArgs());System.out.println("data?validate---begin?to?"+methodName+"?with?"+args);if((Integer)(args.get(0))>0&&(Integer)(args.get(1))>0){System.out.println("data?is?OK");return?true;}else{System.out.println("data?is?bad");return?false;}}} package?com.happBKs.spring.aopbasic.aop1;import?java.util.Arrays;
import?java.util.List;import?org.aspectj.lang.JoinPoint;
import?org.aspectj.lang.ProceedingJoinPoint;
import?org.aspectj.lang.annotation.After;
import?org.aspectj.lang.annotation.AfterReturning;
import?org.aspectj.lang.annotation.AfterThrowing;
import?org.aspectj.lang.annotation.Around;
import?org.aspectj.lang.annotation.Aspect;
import?org.aspectj.lang.annotation.Before;
import?org.springframework.core.annotation.Order;
import?org.springframework.stereotype.Component;@Aspect
@Component
public?class?DataValidateAspect2?{@Before("com.happBKs.spring.aopbasic.aop1.DataValidateAspect.declarePointCut()")public?boolean?beforeMethod(JoinPoint?joinPoint){String?methodName=joinPoint.getSignature().getName();List<Object>?args=Arrays.asList(joinPoint.getArgs());System.out.println("data?validate?2---begin?to?"+methodName+"?with?"+args);if((Integer)(args.get(0))<10000&&(Integer)(args.get(1))<10000){System.out.println("data?is?OK");return?true;}else{System.out.println("data?is?bad");return?false;}}}
配置文件:
<?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-4.1.xsdhttp://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan?base-package="com.happBKs.spring.aopbasic.aop1"></context:component-scan><!--?使用AspectJ注解起作用。自動為匹配的類生成代理對象??--><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
測試代碼:
@Testpublic?void?testSpringAOP(){//1.?創建spring?的?IOC?容器ApplicationContext?ac=new?ClassPathXmlApplicationContext("applicationContext.xml");//2.?從IOC容器獲取bean實例AtithmeticCalculate?atithmeticCalculate?=?(AtithmeticCalculate)ac.getBean(AtithmeticCalculate.class);//考察一下代理對象是否生成System.out.println(atithmeticCalculate.getClass().getName());//3.?使用beanSystem.out.println("Example?1:");int?result=atithmeticCalculate.add(10,?5);System.out.println(result);
// System.out.println("\r\nExample?2:");
// int?result2=atithmeticCalculate.div(10,?0);
// System.out.println(result2);}
輸出結果:
com.sun.proxy.$Proxy10
Example 1:
data validate---begin to add with [10, 5]
data is OK
data validate 2---begin to add with [10, 5]
data is OK
15
轉載于:https://my.oschina.net/happyBKs/blog/483829
總結
以上是生活随笔為你收集整理的Spring框架笔记(二十二)——切点表达式的重用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。