目录
一、Aop认识
1.作用:
2.AOP中关键性概念
二、Aop运用
前置通知
后置通知
环绕通知
异常通知
过滤通知(适配器)
一、Aop认识
------ AOP即面向切面编程
AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。
1.作用:
想象下面的场景,开发中在多个模块间有某段重复的代码,我们通常是怎么处理的?显然,没有人会靠“复制粘贴”吧。在传统的面向过程编程中,我们也会将这段代码,抽象成一个方法,然后在需要的地方分别调用这个方法,这样当这段代码需要修改时,我们只需要改变这个方法就可以了。然而需求总是变化的,有一天,新增了一个需求,需要再多出做修改,我们需要再抽象出一个方法,然后再在需要的地方分别调用这个方法,又或者我们不需要这个方法了,我们还是得删除掉每一处调用该方法的地方。实际上涉及到多个地方具有相同的修改的问题我们都可以通过 AOP 来解决。
2.AOP中关键性概念
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑
通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程 -----非核心业务
代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
二、Aop运用
以书籍管理为例
用前篇博客项目为基础,准备工作
添加以下代码
BookBiz
package com.zking.aop.biz;
public interface BookBiz {
// 购书
public boolean buy(String userName, String bookName, Double price);// 发表书评
public void comment(String userName, String comments);
}
BookBizImpl
package impl;
import com.zking.aop.biz.BookBiz;
import 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 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);
}
}
前置通知
实现org.springframework.aop.MethodBeforeAdvice接口
买书、评论前加系统日志
package com.zking.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactoryBean; 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); } }
rap 画画 看沉香
rap 画画 看沉香
com.zking.aop.biz.BookBiz
com.zking.aop.biz.BookBiz
测试类package com.zking.aop.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.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("小a", "琉璃", 56d);
bean.comment("小a", "对战神更向往了");
}
}
效果图
后置通知
实现org.springframework.aop.AfterReturningAdvice接口
买书返利(存在bug)
package com.zking.aop.advice; import java.lang.reflect.Method; import java.util.Arrays; import org.springframework.aop.AfterReturningAdvice; public class MyAfterReturnAdvice 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); } }
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
rap
画画
看沉香
rap
画画
看沉香
com.zking.aop.biz.BookBiz
myBefore
myAfter
测试类
package com.zking.aop.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.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("小a", "琉璃", 56d);
bean.comment("小a", "对战神更向往了");
}
}
效果图
环绕通知
org.aopalliance.intercept.MethodInterceptor
类似拦截器,会包括切入点,目标类前后都会执行代码。
package com.zking.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.getClass().getName();
//当前调用方法
String methodName=arg0.getMethod().getName();
//当前调用方法所传参数
String args=Arrays.toString(arg0.getArguments());
System.out.println("环绕通知:"+clzName+"."+methodName+"被调用,传递的参数是:"+args);
//方法返回值 执行目标方法buy
Object rs=arg0.proceed();
System.out.println("环绕通知:目标对象方法返回值是"+rs);
return rs;
}}
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
rap
画画
看沉香
rap
画画
看沉香
com.zking.aop.biz.BookBiz
myBefore
myAfter
myMethod
测试
package com.zking.aop.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.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("小a", "琉璃", 56d);
bean.comment("小a", "对战神更向往了");
}
}
效果图
异常通知
org.springframework.aop.ThrowsAdvice
出现异常执行系统提示,然后进行处理。价格异常为例
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
rap
画画
看沉香
rap
画画
看沉香
com.zking.aop.biz.BookBiz
myBefore
myAfter
myMethod
myThrow
package com.zking.aop.advice;
import org.springframework.aop.ThrowsAdvice;
public class MyThrowAdvice implements ThrowsAdvice{
public void after(PriceException p) {//错误示范
System.out.println("异常通知:当价格发生改变,那么执行此处代码块");
}
}
package com.zking.aop.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zking.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("小a", "琉璃", -56d);//测试价格低于0
bean.comment("小a", "对战神更向往了");
}
}
错误效果图
把方法名改正后
package com.zking.aop.advice;
import org.springframework.aop.ThrowsAdvice;
import exception.PriceException;
public class MyThrowAdvice implements ThrowsAdvice{
public void afterThrowing(PriceException p) {
System.out.println("异常通知:当价格发生改变,那么执行此处代码块");
}
}
正确效果图
过滤通知(适配器)
过滤通知(适配器)
org.springframework.aop.support.RegexpMethodPointcutAdvisor
处理买书返利的bug
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
rap
画画
看沉香
rap
画画
看沉香
com.zking.aop.biz.BookBiz
myBefore
myafterPlus
myMethod
myThrow
效果图