课程目标
1、aop的简介及解决的问题
2、前置通知
3、后置通知
4、环绕通知
5、异常通知
6、过滤通知
通过以下案例进行讲解:
前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志
后置通知
实现org.springframework.aop.AfterReturningAdvice接口
买书返利(存在bug)
环绕通知
org.aopalliance.intercept.MethodInterceptor
类似拦截器,会包括切入点,目标类前后都会执行代码。
异常通知
org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
过滤通知(适配器)
org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
aop的简介
解决的问题:解决了需求的改变,造成了原有没必要改变的代码,需要去改变它;
比如:书籍的增删改,本身只需要完成增删改的功能即可,这是如果需要添加日志功能,那么需要在原有的代码基础上,去修改添加日志功能,受牵连的方法就三个(add/edit/del)了;
AOP中关键性概念
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
案例大圖:
前置通知
目标
package com.zking.aop.biz.impl;
import com.zking.aop.biz.IBookBiz;
import com.zking.aop.exception.PriceException;
public class BookBizImpl implements IBookBiz {
public BookBizImpl() {
super();
}
public boolean buy(String userName, String bookName, Double price) {
// 通过控制台的输出方式模拟购书
if (null == price || price <= 0) {
throw new PriceException("book price exception");
}
System.out.println(userName + " buy " + bookName + ", spend " + price);
return true;
}
public void comment(String userName, String comments) {
// 通过控制台的输出方式模拟发表书评
System.out.println(userName + " say:" + comments);
}
}
package com.zking.aop.biz;
public interface IBookBiz {
// 购书
public boolean buy(String userName, String bookName, Double price);
// 发表书评
public void comment(String userName, String comments);
}
通知
package com.zking.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
/**
* 买书、评论前加系统日志
* @author Administrator
*
*/
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
// 在这里,可以获取到目标类的全路径及方法及方法参数,然后就可以将他们写到日志表里去
String target = arg2.getClass().getName();
String methodName = arg0.getName();
String args = Arrays.toString(arg1);
System.out.println("【前置通知:系统日志】:"+target+"."+methodName+"("+args+")被调用了");
}
}
<!-- 目标 -->
<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz"></bean>
<!-- 前置通知 -->
<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean>
<!-- 利用目标+通知生成代理对象 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
</list>
</property>
</bean>
测试
package com.zking.aop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.aop.biz.IBookBiz;
public class AopTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
// IBookBiz bean = (IBookBiz) applicationContext.getBean("bookBiz");
IBookBiz bean = (IBookBiz) applicationContext.getBean("bookProxy");
bean.buy("张三", "Java编程思想", 9.9);
bean.comment("张三", "太深奥了,看不懂");
}
}
更改IBookBiz bean=(IBookBiz) applicationContext.getBean(“bookProxy”);
没有更改原有的方法,即面向切面编程,无疑这样比较灵活
后置通知
目的:实现买书返利的功能
package com.zking.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
/**
* 买书返利
* @author Administrator
*
*/
public class MyAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
String target = arg3.getClass().getName();
String methodName = arg1.getName();
String args = Arrays.toString(arg2);
System.out.println("【后置通知:买书返利】:"+target+"."+methodName+"("+args+")被调用了,"+"该方法被调用后的返回值为:"+arg0);
}
}
配置到spring上下文中
<!-- 后置通知 -->
<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfterReturningAdvice"></bean>
<!-- 利用目标+通知生成代理对象 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
</list>
</property>
</bean>
测试结果
环绕通知
package com.zking.aop.advice;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 环绕通知
* 包含了前置和后置通知
*
* @author Administrator
*
*/
public class MyMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
String target = arg0.getThis().getClass().getName();
String methodName = arg0.getMethod().getName();
String args = Arrays.toString(arg0.getArguments());
System.out.println("【环绕通知调用前:】:"+target+"."+methodName+"("+args+")被调用了");
// arg0.proceed()就是目标对象的方法
Object proceed = arg0.proceed();
System.out.println("【环绕通知调用后:】:该方法被调用后的返回值为:"+proceed);
return proceed;
}
}
Spring上下文配置
<!-- 环绕通知 -->
<bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMethodInterceptor"></bean>
<!-- 利用目标+通知生成代理对象 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
<value>myMethodInterceptor</value>
</list>
</property>
</bean>
测试结果
异常通知
package com.zking.aop.advice;
import org.springframework.aop.ThrowsAdvice;
import com.zking.aop.exception.PriceException;
/**
* 出现异常执行系统提示,然后进行处理。价格异常为例
* @author Administrator
*
*/
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(PriceException ex) {
System.out.println("【异常通知】:当价格发生异常,那么执行此处代码块!!!");
}
}
Spring上下文配置
<!-- 异常通知 -->
<bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrowsAdvice"></bean>
<!-- 利用目标+通知生成代理对象 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myAfterReturningAdvice</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
</list>
</property>
</bean>
测试结果如下
过滤通知
需求:处理买书返利的bug
<!-- 过滤通知 -->
<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterReturningAdvicePlus">
<property name="advice" ref="myAfterReturningAdvice"></property>
<property name="pattern" value=".*buy"></property>
</bean>
<!-- 利用目标+通知生成代理对象 -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
<property name="target" ref="bookBiz"></property>
<property name="proxyInterfaces">
<list>
<value>com.zking.aop.biz.IBookBiz</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<!-- <value>myAfterReturningAdvice</value> -->
<value>myAfterReturningAdvicePlus</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
</list>
</property>
</bean>
測試: