AOP Aspect Oriented Programming 面向切面編程。
OOP Object Oriented Programming 面向?qū)ο缶幊?/p>
1、AOP 加入4個(gè)特殊的jar包
spring-aop-4.2.5.RELEASE.jar
spring-aspects-4.2.5.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
2、在xml文件里引入aop命名空間
<!-- 使用Aspectj配置起作用 為注解的Java 類(真正想要執(zhí)行的類)生成代理對(duì)象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、對(duì)切面類添加注解
@Component//不僅需要將此類加入到AOC容器里面
@Aspect//還需要將其申明為一個(gè)切面
4、如需在調(diào)用真正的類的方法之前執(zhí)行當(dāng)前切面類的方法,則 需對(duì)其添加如下注解,即前置通知
@Before("execution(public int com.xiyou.aop.helloword.Calculate.add(int , int))")
注:都是根據(jù)真正的類的方法寫的,可以從方法那拷過來
@Before("execution(修飾符 返回值 包名.接口名(上面方法沒有).類名(參數(shù)類型)")
可以使用*號(hào)代替上面的一切 如:
@Before("execution(* 包名.接口名(上面方法沒有).*(..)")
第一個(gè)*表示任意修飾符、任意返回值
第二個(gè)*任意類
..代表可變參
再如最強(qiáng)的:
@Before("execution(* *.*(..)")
第一個(gè)*代表匹配任意修飾符及任意返回值
第二個(gè)*代表任意類的對(duì)象
第三個(gè) * 代表任意方法
參數(shù)列表中的 .. 匹配任意數(shù)量的參數(shù)
5、得到該連接點(diǎn)的方法的方法名和參數(shù)值
即定義一個(gè)JoinPoint,可以獲取該方法的細(xì)節(jié)
@Before("execution(* *.*(..)")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.*asList*(joinPoint.getArgs());
}
6、聲明切入點(diǎn)表達(dá)式使代碼更簡潔使用
使用@Pointcut 來聲明切入點(diǎn)表達(dá)式
/**
* 定義一個(gè)方法, 用于聲明切入點(diǎn)表達(dá)式.
* 該方法中可以添入其他常用的代碼.
* 后面的其他通知直接使用方法名來引用當(dāng)前的切入點(diǎn)表達(dá)式.
*/
@Pointcut("execution(* 包名.*(..))")
public void declareJointPointExpression(){}
/*
* 后面的其他通知直接使用方法名來引用當(dāng)前的切入點(diǎn)表達(dá)式.
*/
@Before("declareJointPointExpression()")
7、同理有其它通知
為了簡便使用的上面申明的@Pointcut
1)、@After后置通知,在方法結(jié)束之后執(zhí)行(出現(xiàn)異常也會(huì)執(zhí)行)
2)、@ArterRunning返回通知,在方法返回結(jié)果之后執(zhí)行,可以訪問到方法的返回值
格式:
@AfterReturning(value=execution("方法名"),returning="result")
e.g.
@AfterReturning(value="declareJointPointExpression()",returning="result")
public void afterReturning(JoinPoint joinPoint, Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
3)、@AfterThrowing異常通知,在方法出現(xiàn)異常后執(zhí)行
e.g.
@AfterThrowing(value="declareJointPointExpression()",throwing="e")
public void afterThrowing(JoinPoint joinPoint, Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}
4)、@Around圍繞著方法執(zhí)行 最強(qiáng)大的 但不是最常用的
/* 環(huán)繞通知需要攜帶 ProceedingJoinPoint 類型的參數(shù).
* 環(huán)繞通知類似于動(dòng)態(tài)代理的全過程: ProceedingJoinPoint 類型的參數(shù)可以決定是否執(zhí)行目標(biāo)方法
* 且環(huán)繞通知必須有返回值, 返回值即為目標(biāo)方法的返回值
* ProceedingJoinPoint . 它是 JoinPoint 的子接口
*/
@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName();
try {
//前置通知
System.out.println("The method " + methodName +
" begins with " + Arrays.asList(pjd.getArgs()));
//執(zhí)行目標(biāo)方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//異常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
注意:
在環(huán)繞通知中需要明確調(diào)用ProceedingJoinPoint 的 proceed() 方法來執(zhí)行被代理的方法
如果忘記這樣做就會(huì)導(dǎo)致通知被執(zhí)行了, 但目標(biāo)方法沒有被執(zhí)行
環(huán)繞通知的方法需要返回目標(biāo)方法執(zhí)行之后的結(jié)果
即調(diào)用joinPoint.proceed(); 的返回值, 否則會(huì)出現(xiàn)空指針異常
8、切面的優(yōu)先級(jí)用
@Order(index)注解到切面類 index 越小優(yōu)先級(jí)越高
9、基于配置文件xml
<!-- 先將用到的類交給IOC 容器管理 -->
<bean id="calculate" class="com.xiyou.aop.helloword.xml.Calculate"></bean>
<bean id="loggingAspect" class="com.xiyou.aop.helloword.xml.LoggingAspect"></bean>
<aop:config>
<!-- 配置切面表達(dá)式 -->
<aop:pointcut expression="execution(* 包名.*(int , int))" id="pointcut"/>
<!-- 配置前面通知 -->
<aop:aspect ref="loggingAspect" order="2" >
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
同理可以配置其它通知