• 代理模式


    静态代理略过,哈哈

    主要来说动态代理

    1、jdk实现的动态代理

    有两个主要类,一个是Proxy,一个是InvocationHandler

    public interface UserService {
    
    	public void add();
    	public void delete();
    }
    
    public class UserServiceImpl implements UserService {
    
    	@Override
    	public void add() {
    		System.out.println("执行了add方法");
    	}
    
    	@Override
    	public void delete() {
    		System.out.println("执行了delete方法");
    	}
    }
    
    public class ProxyInvocationHandler implements InvocationHandler {
    	
    	//被代理的真实对象
    	private Object target;
    
    	public void setTarget(Object target) {
    		this.target = target;
    	}
    	
    	//生成得到代理类
    	public Object getProxy(){
    		return Proxy.newProxyInstance(this.getClass().getClassLoader(), 
    				target.getClass().getInterfaces(), this);
    	}
    
    	@Override
    	public Object invoke(Object proxy, Method method, Object[] args)
    			throws Throwable {
    		beforeLog(method.getName());
    		Object result = method.invoke(target, args);
    		afterLog(method.getName());
    		return result;
    	}
    	
    	public void beforeLog(String methodName){
    		System.out.println("开始执行【" + methodName + "】方法。");
    	}
    	public void afterLog(String methodName){
    		System.out.println("执行了【" + methodName + "】方法结束。");
    	}
    }
    
    public class Client {
    
    	public static void main(String[] args) {
    		UserServiceImpl userServiceImpl = new UserServiceImpl();
    		ProxyInvocationHandler invocationHandler = new ProxyInvocationHandler();
    		invocationHandler.setTarget(userServiceImpl);
    		UserService proxy = (UserService) invocationHandler.getProxy();
    		proxy.add();
    	}
    }
    

    输出结果
    image

    2、cglib实现的动态代理

    引入依赖

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm</artifactId>
        <version>7.1</version>
    </dependency>
    
    public class CglibProxy implements MethodInterceptor{
    
    	private Object target;
    	
    	public Object getInstance(final Object target){
    		this.target = target;
    		Enhancer enhancer = new Enhancer();
    		enhancer.setSuperclass(this.target.getClass());
    		enhancer.setCallback(this);
    		return enhancer.create();
    	}
    	
    	@Override
    	public Object intercept(Object object, Method method, Object[] args,
    			MethodProxy proxy) throws Throwable {
    		beforeLog(method.getName());
    		Object result = method.invoke(target, args);
    		afterLog(method.getName());
    		return result;
    	}
    	public void beforeLog(String methodName){
    		System.out.println("开始执行【" + methodName + "】方法。");
    	}
    	public void afterLog(String methodName){
    		System.out.println("执行了【" + methodName + "】方法结束。");
    	}
    }
    
    public class Client {
    
    	public static void main(String[] args) {
    		
    		UserServiceImpl userServiceImpl = new UserServiceImpl();
    		CglibProxy cglibProxy = new CglibProxy();
    		UserService instance = (UserService) cglibProxy.getInstance(userServiceImpl);
    		instance.add();
    	}
    }
    

    输出结果
    image

  • 相关阅读:
    一个应用程序无法启动错误的解决过程
    C#调用C库的注意事项
    STM32硬件调试详解
    CP2102模块介绍(USB转uart)
    CH340在STM32实现一键下载电路
    LM27313升压转换器
    常用贴片电阻、电容、电感封装
    MAX16054
    在51系列中data,idata,xdata,pdata的区别
    用UGN3503霍尔器件制作的数字指南针_电路图
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/14599592.html
Copyright © 2020-2023  润新知