AOP(Aspect Oriented Programming)通俗一点说,就是不改变源代码,还给类增加新的功能
切面 通知 连接点 切入点 目标对象 引入 目标对象 代理对象 织入
1.切面 :要实现的交叉功能,是系统模块化的一个切面或领域。如日志纪录。
2.连接点:应用程序执行过程中插入切面的点,可以是方法调用,异常抛出,或者要修改字段。
3.通知:切面的实际实现,他通知系统新的行为。如在日志通知包含了实 现日志功能的代码,如向日志文件写日志。通知在连接点插入到应用系统中。
通知 | 类型 | 接口描述 |
Around | MethodInterceptor | 拦截对目标方法调用 |
Before | BeforeAdvice | 在目标方法调用前调用 |
After | AfterReturningAdvice | 在目标方法调用后调用 |
Throws | ThrowsAdvice | 当目标方法抛出异常时调用 |
4.切入点:定义了通知应该应用在哪些连接点,通知可以应用到AOP框架支持的任何连接点。
5.引入:为类添加新方法和属性。
6.目标对象:被通知的对象。既可以是你编写的类也可以是第三方类。
7.代理:将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而 改变。
8.织入:将切面应用到目标对象从而创建一个新代理对象的过程。织入发生在目标 对象生命周期的多个点上:
1) 编译期:切面在目标对象编译时织入.这需要一个特殊的编译器。
2) 类装载期:切面在目标对象被载入JVM时织入.这需要一个特殊的类载入器。
3)运行期:切面在应用系统运行时织入。
以下是一个例子
public interface WelcomeService {
public String sayHello(String name);
public String throwsTest();
}
public class WelcomeServiceImpl implements WelcomeService{
public String sayHello(String name) {
System.out.println("Hi,I'm "+name);
return name;
}
public String throwsTest() {
int i = 9/0;
return null;
}
}
/**后置通知**/
public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("AfterReturningAdvice\nreturnValue:"+returnValue);
}
}
/**前置通知**/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("Method Before Advice");
}
}
/**环绕通知**/
public class MyMethodInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("MethodInterceptor:begin");
Object proceed = invocation.proceed();
System.out.println("MethodInterceptor:end");
return proceed;
}
}
/**
异常通知
public interface ThrowsAdvice{
}
该接口为标识性接口,没有任何方法,但实现该接口的类必须要有如下形
式的方法:
void afterThrowing(Throwable throwable);
void afterThrowing(Method m,Object[] os,Object
target,Throwable throwable);
第一个方法只接受一个参数:需要抛出的异常。
第二个方法接受异常、被调用的方法、参数以及目标对象。
**/
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Throwable throwable) {
System.out.println(throwable.getMessage());
}
public void afterThrowing(Method m,Object[] os,Object
target,Throwable throwable) {
}
}
ApplicationContext ac = new ClassPathXmlApplicationContext("study/spring/aop/advice1.xml");
WelcomeService ws = (WelcomeService)ac.getBean("welcomeService");
ws.sayHello("zhangsan");
//ws.throwsTest();
<!-- 前置通知 -->
<bean id="myMethodBeforeAdvice" class="study.spring.aop.advice.MyMethodBeforeAdvice"></bean>
<!-- 后置通知 -->
<bean id="myAfterReturningAdvice" class="study.spring.aop.advice.MyAfterReturningAdvice"></bean>
<!-- 环绕通知 -->
<bean id="myMethodInterceptor" class="study.spring.aop.advice.MyMethodInterceptor"></bean>
<!-- 异常通知 -->
<bean id="myThrowsAdvice" class="study.spring.aop.advice.MyThrowsAdvice"></bean>
<!-- 目标对象 -->
<bean id="welcomeServiceTarget" class="study.spring.aop.WelcomeServiceImpl"></bean>
<!-- 对象代理 -->
<bean id="welcomeService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>study.spring.aop.WelcomeService</value>
</list>
</property>
<!-- 拦截器名集 -->
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
</list>
</property>
<!-- 指定目标对象 -->
<property name="target" ref="welcomeServiceTarget"></property>
</bean>
运行结果:
Method Before Advice
MethodInterceptor:begin
Hi,I'm zhangsan
MethodInterceptor:end
AfterReturningAdvice
returnValue:zhangsan