目录
一、Aop所解决的问题
二、Aop的关键名词介绍
三、前置通知
四、后置通知
五、环绕通知
六、异常通知
七、过滤通知
八、知识总结
一、Aop所解决的问题
AOP:面向切面编程
二、Aop的关键名词介绍解决的问题:
解决了需求的改变,造成了原有没必要改变的代码,需要去改变它;
比如:书籍的增删改,本身只需要完成增删改的功能即可,这是如果需要添加日志功能,那么需要在原有的代码基础上,去修改添加日志功能,受牵连的方法就三个(add/edit/del)了。
如书籍的增删改查:
需求1:要求针对于书籍的增删改查进行监管,添加日志功能
需求2:除了书籍管理的增删改查要添加日志功能,其他管理板块都要
思考:不需要在原有的业务逻辑中添加任何代码,可以实现共性非业务功能
连接点(Joinpoint):
程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):
被通知(被代理)的对象
(注1:完成具体的业务逻辑)
通知(Advice):
在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
(注2:完成切面编程)
代理(Proxy):
将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
(注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的)
切入点(Pointcut):
多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):
适配器=通知(Advice)+切入点(Pointcut)
三、前置通知以书籍的增删改查为例,
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志。
首先准备工作:
BookBiz :
package com.ycx.aop.biz; public interface BookBiz { // 购书 public boolean buy(String userName, String bookName, Double price); // 发表书评 public void comment(String userName, String comments); }
BookBizImpl :
package com.ycx.aop.biz.impl; import com.ycx.aop.biz.BookBiz; import com.ycx.aop.exception.PriceException; public class BookBizImpl implements BookBiz { 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); } }
PriceException :
package com.ycx.aop.exception; public class PriceException extends RuntimeException { public PriceException() { super(); } public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public PriceException(String message, Throwable cause) { super(message, cause); } public PriceException(String message) { super(message); } public PriceException(Throwable cause) { super(cause); } }
spring-context.xml:
MyMethodBeforeAdvice :
com.ycx.aop.biz.BookBiz
myBefore
package com.ycx.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.MethodBeforeAdvice; public class MyMethodBeforeAdvice implements MethodBeforeAdvice{ @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { // 目标对象的类名 String clzName=arg2.getClass().getName(); // 当前调用的方法是 String methodName=arg0.getName(); // 当前调用方法所传递参数 String args=Arrays.toString(arg1); System.out.println("【系统日志】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args); } }Demo1 :
package com.ycx.aop.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ycx.aop.biz.BookBiz; public class Demo1 { @SuppressWarnings("resource") public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml"); // BookBiz bean =(BookBiz) context.getBean("bookBiz"); BookBiz bean =(BookBiz) context.getBean("bookProxy"); bean.buy("杨总", "一日暴富", 999); bean.comment("阿颜", "好"); } }四、后置通知
实现org.springframework.aop.AfterReturningAdvice接口
买书返利(存在bug)
MyAfterReturningAdvice :
package com.ycx.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.AfterReturningAdvice; public class MyAfterReturningAdvice implements AfterReturningAdvice{ @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { // 目标对象的类名 String clzName=arg3.getClass().getName(); // 当前调用的方法是 String methodName=arg1.getName(); // 当前调用方法所传递参数 String args=Arrays.toString(arg2); System.out.println("【买书返利】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args+";目标对象方法返回值为:"+arg0); } }
spring-context.xml:
五、环绕通知
myBefore myAfter
org.aopalliance.intercept.MethodInterceptor
类似拦截器,会包括切入点,目标类前后都会执行代码。
MyMethodInterceptor :
package com.ycx.aop.advice; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyMethodInterceptor implements MethodInterceptor{ @Override public Object invoke(MethodInvocation arg0) throws Throwable { // 目标对象的类名 String clzName=arg0.getThis().getClass().getName(); // 当前调用的方法是 String methodName=arg0.getMethod().getName(); // 当前调用方法所传递参数 String args=Arrays.toString(arg0.getArguments()); System.out.println("【环绕通知】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args); //方法的返回值 执行目标方法 bookBiz.buy(...,...,...); Object rs = arg0.proceed(); System.out.println("【环绕通知】:目标对象方法返回值为:"+rs); return rs; } }
spring-context.xml:
六、异常通知
myBefore myAfter myMethod
org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
MyThrowsAdvice :
package com.ycx.aop.advice; import org.springframework.aop.ThrowsAdvice; import com.ycx.aop.exception.PriceException; public class MyThrowsAdvice implements ThrowsAdvice{ public void afterThrowing(PriceException p) { System.out.println("【异常通知】:当前价格发生异常,那么执行此处代码块"); } }
spring-context.xml:
七、过滤通知
myBefore myAfter myMethod myThrows
org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
spring-context.xml:
myBefore myAfterPlus myMethod myThrows
综上所有运行效果:
八、知识总结 Aop面向切面编程 主要作用:
将核心的业务功能与非核心的业务功能进行分离;
将核心的业务功能写到目标对象中,将非核心的业务功能写到通知中
专业名词:通知、连接点、目标对象、切入点、代理、适配器
日常开发中通常的应用场景:事务管理、日志
事务的开启:前置通知
事务的提交:后置通知
事务的回滚:异常通知