博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代理的四种实现方式
阅读量:4668 次
发布时间:2019-06-09

本文共 3961 字,大约阅读时间需要 13 分钟。

1、jdk动态代理 实现

核心:Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this); 

package org.sh.proxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import org.sh.HellowImpl;import org.sh.HellowInf;public class JDKProxyFactory implements InvocationHandler {	private Object target = null;	public Object getProxyBean(Object target){		this.target = target;		return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this);		 	}	@Override	public Object invoke(Object proxy, Method method, Object[] args)			throws Throwable {		System.out.println("i am proxy");		Object result = null;		if(((HellowInf)this.target).getName() != null){			System.out.println("i am not null");			try{				//前置				result = method.invoke(this.target, args); //注意 this.target 而不是proxy				//后置			}catch(Exception e){				//例外			}finally{				//最终			}			 		}			return null;	}		public static void main(String[] args) {				Object obj = new JDKProxyFactory().getProxyBean(new HellowImpl("wch"));		HellowInf hellow = (HellowInf) obj;		hellow.sayHellow();	}}
 

2、cglib 代理(导入相应的包,spring会包含 cglib-nodep.jar)

核心:Enhancer enhancer = new Enhancer();

enhancer.setSuperclass(target.getClass()); //父类 
enhancer.setCallback(this);  //回调
return enhancer.create();

package org.sh.proxy;import java.lang.reflect.Method;import org.sh.HellowImpl;import org.sh.HellowInf;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;public class CGLIBProxyFactory  implements MethodInterceptor{	private Object target;	public Object createProxyBean(Object target){		this.target = target;		Enhancer enhancer = new Enhancer();		enhancer.setSuperclass(target.getClass()); //父类 		enhancer.setCallback(this);  //回调		return enhancer.create();	}	@Override	public Object intercept(Object target, Method method, Object[] arg2,			MethodProxy methodProxy) throws Throwable {		HellowInf hellow = (HellowInf) this.target;		if (hellow.getName()!= null) {			System.out.println("cglib");			return methodProxy.invoke(this.target, arg2);		}		return null;	}	public static void main(String[] args) {		Object obj = new CGLIBProxyFactory().createProxyBean(new HellowImpl("wch"));		HellowInf hellow = (HellowInf) obj;		hellow.sayHellow();	}	}

3、aspect 注解方式

package org.sh.aop;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;@Aspectpublic class Myinterceptor {	@Pointcut("execution(* org.sh.aop.PersonBean.*(..))")	public void anyMethod(){			}	@Before("anyMethod() && args(param)")	public void before(String param){		//只有一个String类型的参数的方法才会被拦截		System.out.println("before");	}	@AfterReturning(pointcut="anyMethod()",returning="result")	public void afterReturn(String result){		//返回stirng类型的result才会被拦截	}	@AfterThrowing(pointcut="anyMethod()",throwing="e")	public void afterThrow(Exception e){		//e就是方法抛出的异常	}	@After("anyMethod()")	public void after(){		//最终通知	}	@Around("anyMethod()")	public Object around(ProceedingJoinPoint pjp){		try {			//之前			return pjp.proceed(); //执行方法			//之后		} catch (Throwable e) {			e.printStackTrace();		}		return null;	}	public static void main(String[] args) {		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");		((PersonBean)ac.getBean("personbean")).save();	}}

4、配置文件

转载于:https://www.cnblogs.com/vvch/p/4027593.html

你可能感兴趣的文章
对数组序列进行洗牌
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
win7,Ubuntu 12.04 双系统修改启动项顺序三方法
查看>>
python--列表推导式和生成表达式
查看>>
P - Psychos in a Line 单调队列
查看>>
POJ 2653 Pick-up sticks(计算几何)
查看>>
大型网站高并发的架构演变图-摘自网络
查看>>
8丶运行及总结
查看>>
Unity获取手机的电量时间
查看>>
Spring框架:Spring容器具体解释
查看>>
MongoDB 3.2 从安装到使用。
查看>>
sqlplus登录、连接命令
查看>>
C#简单线程同步例子
查看>>
VC++与MySQL数据库的连接(Window)
查看>>
将查询列表内容保存到excel表格中,并保存到相应的盘中
查看>>
python requests提示警告InsecureRequestWarning
查看>>
三步解决 vue 按需加载
查看>>
准备在CSDN知识库建立一个Ext JS的知识库
查看>>