• Spring(AOP)


    一、AOP的简介

    解决的问题:解决了需求的改变,造成了原有没必要改变的代码,需要去改变它;比如:书记的增删改,本身只需要完成增删改的功能即可,这是如果需要添加日志功能,那么需要在原有的代码基础上,去修改添加日志功能,受牵连的方法就三个(add/edit/del)了

    二、AOP中关键名词概念

    连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.

    目标(Target):被通知(被代理)的对象
    注1:完成具体的业务逻辑

    通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
    注2:完成切面编程

    代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
    例子:外科医生+护士 注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

    切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
    (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)

     适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
    
    • 1

    三、AOP案例讲解

    3.1 前置通知

    加入我们的接口类,实现类和异常类
    在这里插入图片描述

    BookBiz:

    public interface BookBiz {
    	// 购书
    	public boolean buy(String userName, String bookName, Double price);
    
    	// 发表书评
    	public void comment(String userName, String comments);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    BookBIzImpl:

    kBiz {
    
    	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);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    PriceException:

    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);
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在我们配置文件里配置
    在这里插入图片描述
    再建一个包放通知
    在这里插入图片描述

    /**
     * 前置通知
     * 	需求:买书、评论前加系统日志
     * @author Administrator
     *
     */
    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);
    		
    	}
    
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    进入spring-context.xml配置

    
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">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.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    			list>
    		property>
    	bean>
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    创建测试类:
    Demo1:

    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    		BookBiz bean = (BookBiz) context.getBean("bookBiz");
    		
    		bean.buy("zhangsan", "老人与海", 6.6d);
    		bean.comment("zhangsan", "好看。。。。。");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试结果:
    在这里插入图片描述
    当我们改变一下:
    在这里插入图片描述
    测试结果:
    调用了通知里的方法,加到日志里
    在这里插入图片描述

    3.2 后置通知

    创建一个后置通知类,实现后置通知
    我们可以看到后置通知比前置通知多一个参数
    在这里插入图片描述
    其中后置通知肯定包含前置通知,我们把前置对象(MyMethodBeforeAdvice)里的调过去

    后置对象:MyAfterReturningAdvice:

    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);
    		
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    第一个参数方法调用后的返回值:
    在这里插入图片描述
    后置通知MyAfterReturningAdvice:

    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);
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    spring-context.xml配置:
    在这里插入图片描述
    进入Demo1进行测试:
    系统日志:每个方法调用前都有
    买书返利:系统调用后

    在这里插入图片描述

    3.3 环绕通知

    包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
    它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)

    创建一个环绕通知类:
    环绕通知=前置通知和后置通知
    环绕通知参数=后置通知四个参数
    getThis指的是目标对象
    环绕通知具有返回值

    /**
     * 环绕通知=前置通知+后置通知
     * 环绕通知所实现的MethodInterceptor有两个:intercept、proxy两者都可以,区别第一个需要实现接口才能代理,第二个则不需要
     * 
     * @author Administrator
     *
     */
    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(张三,mm,7.8)
    		Object rs = arg0.proceed();
    		System.out.println("【环绕通知:】目标对象方法的返回值为:"+rs);
    		return rs;
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    spring-context.xml配置:
    在这里插入图片描述

    测试:
    环绕通知=前置通知和后置通知,因此两者都打印出来,所以一般我们只需要些环绕通知即可
    在这里插入图片描述

    3.4异常通知

    创建一个异常通知类:
    注意:方法名一定要叫‘afterThrowing’,不然不会进入该方法

    /**
     * 关于异常通知
     * 	相较于前置通知、后置通知、环绕通知有一区别,前面三大通知都需要实现其中的方法
     * 	异常通知则不需要,但是,它的方法名又是固定的,
     * @author Administrator
     *
     */
    public class MyThrowsAdvice implements ThrowsAdvice{
    	public void afterThrowing(PriceException p) {
    		System.out.println("【异常通知】:当价格发送异常,那么执行此处代码块!!");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    spring-context.xml配置:
    在这里插入图片描述
    测试,当我们方法名不叫afterThrowing时:
    在这里插入图片描述
    Demo1价格改成负数:
    在这里插入图片描述
    方法没有显示
    在这里插入图片描述
    测试正确方法:
    控制台会显示改方法
    在这里插入图片描述

    3.5过滤通知

    处理买书返利的bug,因为我们不可能评论一条,商家就给你返利这是不可能的

    我们直接在spring-context.xml配置,给后置通知添加过滤功能,只有买书返利,评论不返利

    在这里插入图片描述
    测试,我们可以看到控制台只出现了一次买书返利,说明我们的bug修复了
    在这里插入图片描述

  • 相关阅读:
    flex布局(弹性盒子三)
    ByteBuffer杂记
    四种自动化测试模型实例及优缺点
    【Java 进阶篇】Java Request 请求转发详解
    Linux —— 基础IO
    [洛谷] P1143 进制转换
    WebDAV之葫芦儿·派盘+恒星播放器
    视频质量评价
    QT页面布局方法大全
    Pytorch学习 day12(模型的保存和加载)
  • 原文地址:https://blog.csdn.net/qq_65345936/article/details/126211965