AOP概念
切面:切面是通知和切入点的结合 连接点: spring允许你使用通知的地方,定义了在应用程序中可以插入其他逻辑程序的点 通知: 说明了什么时候干和干什么
前置通知:连接点之前执行的通知后置通知:连接点退出时执行的通知(不论是正常返回还是异常退出)都会执行返回后通知:连接点正常完成后执行的通知。不包括抛出异常的情况环绕通知:方法调用前后完成自定义的行为 异常通知:方法抛出异常的时候执行的通知
切入点: 定义了在哪里干 织入:是在适当的位置,将切面插入到应用程序的过程 目标对象:被通知的对象 AOP代理:spring Aop中有两种代理方式:JDK动态代理和CGLib代理。默认,目标对象实现了接口,采用JDK,反之采用CGLib,需要将aop:config的proxy-target-class属性设置为true
XML配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
<aop:config>
<aop:pointcut id="foundTiger" expression="execution(* com.picc.spring.aop.Tiger.walk(..))"/>
<aop:aspect id="myAspect" ref="fighter">
<aop:before method="foundBefore" pointcut-ref="foundTiger"></aop:before>
<aop:after method="foundAfter" pointcut-ref="foundTiger"></aop:after>
<aop:after-returning method="foundAfterReturn" pointcut-ref="foundTiger"></aop:after-returning>
<aop:after-throwing method="foundAfterThrowing" pointcut-ref="foundTiger"></aop:after-throwing>
<aop:around method="foundaround" pointcut-ref="foundTiger"></aop:around>
</aop:aspect>
</aop:config>
<bean id="tiger" class="com.picc.spring.aop.Tiger"></bean>
<bean id="fighter" class="com.picc.spring.aop.Fighter"></bean>
</beans>
java实战
package com
.picc
.spring
.aop
;
import org
.aspectj
.lang
.ProceedingJoinPoint
;
public class Fighter {
public void foundBefore() {
System
.out
.println("foundBefore()---前置通知");
}
public void foundAfter() {
System
.out
.println("foundAfter()---后置通知");
}
public void foundAfterReturn() {
System
.out
.println("foundAfterReturn()----正常返回不包括异常的通知");
}
public void foundAfterThrowing() {
System
.out
.println("foundAfterThrowing()----方法抛出异常后执行的通知");
}
public Object
foundaround(ProceedingJoinPoint pjp
) {
System
.out
.println("环绕前开始");
Object proceed
= null
;
Object
[] args
= pjp
.getArgs();
try {
foundBefore();
proceed
= pjp
.proceed(args
);
System
.out
.println(proceed
);
foundAfter();
} catch (Throwable e
) {
e
.printStackTrace();
}
System
.out
.println("环绕前结束");
return proceed
;
}
}
package com
.picc
.spring
.aop
;
public class Tiger {
public int walk(String a
) {
System
.out
.println("Tiger is walking......"+a
);
return 1;
}
}