Spring学习笔记-AOP前传之动态代理

假设有如下需求:

写一个计算器类,里面包含加减乘除四个方法。在每个方法开始前打印出该方法开始的消息,在每个方法结束前打印出该方法结束的消息和计算的结果。

普通方法,先写一个借口,然后在接口里实现四个方法。在每个方法里加上要打印的语句。实现代码如下。

ArithmeticCalculator接口

package com.spring.aop.helloworld;

public interface ArithmeticCalculator {
	int add(int i, int j);

	int sub(int i, int j);

	int mul(int i, int j);

	int div(int i, int j);
}

ArithmeticCalculatorLoggingImpl.java 实现上面的接口

package com.spring.aop.helloworld;

public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator{

	@Override
	public int add(int i, int j) {
		System.out.println("The method add begins with [" + i + ", " + j + "]");
		int result = i + j;
		System.out.println("The method add end with " + result);
		return result;
	}

	@Override
	public int sub(int i, int j) {
		System.out.println("The method sub begins with [" + i + ", " + j + "]");
		int result = i - j;
		System.out.println("The method sub end with " + result);
		return result;
	}

	@Override
	public int mul(int i, int j) {
		System.out.println("The method mul begins with [" + i + ", " + j + "]");
		int result = i * j;
		System.out.println("The method  mul end with " + result);
		return result;
	}

	@Override
	public int div(int i, int j) {
		System.out.println("The method div begins with [" + i + ", " + j + "]");
		int result = i / j;
		System.out.println("The method div end with " + result);
		return result;
	}

}

Main.java

ArithmeticCalculator  arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();

arithmeticCalculator.add(1, 5);
System.out.println("----------");
arithmeticCalculator.sub(5, 3);
System.out.println("----------");
arithmeticCalculator.mul(3, 7);
System.out.println("----------");
arithmeticCalculator.div(9, 3);

程序运行结果:

The method add begins with [1, 5]

The method add end with 6

----------

The method sub begins with [5, 3]

The method sub end with 2

----------

The method mul begins with [3, 7]

The method  mul end with 21

----------

The method div begins with [9, 3]

The method div end with 3

可见,上面的代码中间存在这大量相似的代码。而面向对象编程又不能很好地解决这个问题,下面采用动态代理的方法来解决上面的问题。


接口不变。写一个实现类ArithmeticCalculatorImpl.java  这个实现类只关注业务,没有需要打印的内容

package com.spring.aop.helloworld;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator{

	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

}

ArithmeticCaculatorLogginProxy.java

package com.spring.aop.helloworld;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class ArithmeticCaculatorLogginProxy {
	//要代理的对象
	private ArithmeticCalculator target;

	public ArithmeticCaculatorLogginProxy(ArithmeticCalculator target){
		this.target = target;
	}

	public ArithmeticCalculator getLoggingProxy() {
		ArithmeticCalculator proxy = null;
		//代理对象由哪一个类加载器负责加载
		ClassLoader loader = target.getClass().getClassLoader();
		//代理对象的类型,即其中有哪些方法
		Class[] interfaces = new Class[]{ArithmeticCalculator.class};
		//当调用代理对象其中的方法时,该执行的代码
		InvocationHandler h = new InvocationHandler() {
			/**
			 * proxy:正在返回的代理对象,一般情况下,在invoke方法中都不适用该对象
			 * method:正在被调用的方法
			 * args:调用方法时,传入的参数
			 */
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				//下面这句执行的时候又会调用invoke方法,所以会出现死循环导致内存溢出
				//System.out.println(proxy.toString());
				String methodName = method.getName();
				//日志
				System.out.println("The method " + methodName + " begins with" + Arrays.asList(args));
				System.out.println("Invoke...");
				//执行方法
				Object result = method.invoke(target, args);
				//日志
				System.out.println("The method" + methodName + " ends with " + result);
				return result;
			}
		};
		proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
		return proxy;
	}
}

main方法

ArithmeticCalculator target = new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCaculatorLogginProxy(target).getLoggingProxy();
proxy.add(1, 5);
System.out.println("----------");
proxy.sub(5, 3);
System.out.println("----------");
proxy.mul(3, 7);
System.out.println("----------");
proxy.div(9, 3);

程序运行结果 :

The method add begins with[1, 5]

Invoke...

The methodadd ends with 6

----------

The method sub begins with[5, 3]

Invoke...

The methodsub ends with 2

----------

The method mul begins with[3, 7]

Invoke...

The methodmul ends with 21

----------

The method div begins with[9, 3]

Invoke...

The methoddiv ends with 3

时间: 2024-08-08 16:41:17

Spring学习笔记-AOP前传之动态代理的相关文章

Spring笔记(三)AOP前篇之动态代理

AOP思想是将程序中的业务代码与服务代码进行分离,在运行时进行结合.比较强调程序的层次结构,是一种面向切面的编程.而在AOP实现的底层主要用到了动态代理,而动态代理又分为JDK动态代理和CGLIB动态代理,两者的区别是JDK动态代理的实现中业务类必须必须定义接口,而CGLIB没有这个约束,可以说CGLIB更强大: JDK动态代理实现示例: // 业务接口定义 public interface IUnit {     void execute(String msg); } // 业务实现类 pub

Spring学习笔记AOP(四)

鲁春利的工作笔记,好记性不如烂笔头 基于XML配置方式声明切面 Spring使用org.springframework.aop.Advisor接口表示切面的概念,Advisor表示只有一个通知(org.aopalliance.aop.Advice)和一个切入点(org.springframework.aop.Pointcut)的切面.Advisor可以使用<aop:config>标签下的<aop:advisor>标签定义. <aop:advisor id="标识&q

Spring学习笔记----AOP编程

先用代码讲一下什么是传统的AOP(面向切面编程)编程 需求:实现一个简单的计算器,在每一步的运算前添加日志.最传统的方式如下: Calculator.java package cn.limbo.spring.aop.calculator; /** * Created by Limbo on 16/7/14. */ public interface Calculator { int add(int i , int j); int sub(int i , int j); int mul(int i

Spring学习笔记AOP(二)

鲁春利的工作笔记,好记性不如烂笔头 要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: 引入后AOP命名空间并启动对@AspectJ注解的支持(spring-context-aop-annotation.xml): <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&q

Spring学习笔记AOP(三)

鲁春利的工作笔记,好记性不如烂笔头 基于XML配置方式声明切面

Spring学习笔记之六(数据源的配置)

1.前言 上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客.来解说一下Spring中的数据源的配置.  2.DAO支持的模板类 Spring提供了非常多关于Dao支持的模板类,比如HibernateTemplate.JdbcTemplate等,以下以后者为例.来看一个Demo <span style="font-family:SimSun;font-size:18px;">package com.test; import org.springfr

spring学习笔记(19)mysql读写分离后端AOP控制实例

在这里,我们接上一篇文章,利用JNDI访问应用服务器配置的两个数据源来模拟同时操作不同的数据库如同时操作mysql和oracle等.实际上,上个例子可能用来模拟mysql数据库主从配置读写分离更贴切些.既然如此,在本例中,我们就完成读写分离的模拟在web端的配置实例. 续上次的例子,关于JNDI数据源的配置和spring datasource的配置这里不再重复.下面着重加入AOP实现DAO层动态分库调用.可先看上篇文章<spring学习笔记(18)使用JNDI模拟访问应用服务器多数据源实例 >

【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.com/ssslinppp/p/4607043.html (请参考).本文主要讲多文件上传的过程. 主要区别在于控制层代码不同,同时,jsp代码也有相应修改. 2. 添加jar包 commons-fileupload-1.2.2.jar: commons-io-2.0.1.jar: 3. 配置CommonsMul

不错的Spring学习笔记(转)

Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是spring.jar和commons-logging.jar.此外为了便于测试加入了JUnit包.   在Myeclipse中创建Java项目.   编写一个接口类,为了简单,只加入了一个方法.   Java代码   1.package com.szy.spring.interfacebean;