Java静态代理和动态代理总结
静态代理
第一种实现(基于接口):
1》接口
public interface Hello { void say(String msg); }
2》目标类,至少实现一个接口
public class HelloImpl implements Hello { public void say(String msg) { System.out.println("Hi,"+msg); } }
3》代理类(与目标类实现相同接口,从而保证功能一致)
public class HelloProxy implements Hello{ private Hello hello; public HelloProxy(Hello hello){ this.hello = hello; } public void say(String msg){ before(); hello.say(msg); after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3》测试
/** * @Author LZHL * @Create 2017-02-19 10:26 * @Description */ public class Main { public static void main(String[] args) throws Exception { HelloImpl target = new HelloImpl(); HelloProxy proxy = new HelloProxy(target); proxy.say("LZHL"); } }
第二种实现(基于目标类):
1>目标类
public class HelloTarget { public void sayHello(String name){ System.out.println("Hi,"+name); } }
2>代理类(通过继承目标类,保证功能一致)
public class HelloProxy extends HelloTarget{ private HelloTarget target; public HelloProxy(HelloTarget target){ this.target = target; } @Override public void sayHello(String name) { this.before(); target.sayHello(name); this.after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3>测试
public class Main { public static void main(String[] args) throws Exception { HelloTarget target = new HelloTarget(); HelloProxy proxy= new HelloProxy(target); proxy.sayHello("LZHL"); } }
动态代理
动态代理的代理类是在程序运行期间动态生成的,也有两种实现,一种是JDK动态代理,一种是CGLib动态代理
1》JDK动态代理(基于接口实现,与目标类实现相同接口,从而保证功能一致)
/** * @Author LZHL * @Create 2017-02-19 12:46 * @Description */ public class Main { public static void main(String[] args){ final HelloImpl target = new HelloImpl(); Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { /* * proxy: 代理对象 * method: 目标对象的方法对象 * args: 目标对象方法的参数 * return: 目标对象方法的返回值 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before"); Object retValue = method.invoke(target, args); System.out.println("after"); return retValue; } }); Hello proxy = (Hello) proxyInstance; proxy.say("LYX"); //可以把InvocationHandler提取出来,单独写一个类,为了方便大家看,这里我用内部类的形式 class JDKProxy implements InvocationHandler { private Object target; public JDKProxy(Object target){ this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { before(); Object result = method.invoke(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } InvocationHandler ih = new JDKProxy(target); Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih); Hello proxy2 = (Hello) proxyInstance2; proxy2.say("LZHL"); } }
2》CGLib动态代理(基于目标类,通过继承目标类,从而保证功能一致),需要导入cglib-3.2.4.jar包
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency> </dependencies>
1)目标类
public class Hi { public void sayHi(String msg){ System.out.println("Hi,"+msg); } }
2)测试
/** * @Author LZHL * @Create 2017-02-19 13:19 * @Description */ public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); //设置父类 enhancer.setSuperclass(Hi.class); //设置回调函数 enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("before"); Object retValue = methodProxy.invokeSuper(target, args); System.out.println("after"); return retValue; } }); Object proxy = enhancer.create(); Hi hi = (Hi) proxy; hi.sayHi("LXY"); //可以把MethodInterceptor提取出来,单独写一个类,为了方便大家看,这里我用内部类的形式 class CGLibProxy implements MethodInterceptor { public <T> T getProxy(Class<T> clazz){ return (T) Enhancer.create(clazz, this); } public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { before(); Object result = proxy.invokeSuper(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } CGLibProxy cgLibProxy = new CGLibProxy(); Hi hi2 = cgLibProxy.getProxy(Hi.class); hi2.sayHi("LZHL"); } }
以上所述是小编给大家介绍的Java静态代理和动态代理总结,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
相关文章
java MultipartFile文件上传重命名详细代码示例
在文件上传功能开发中,为防止文件重名导致数据覆盖,常见的做法是在文件名前加上UUID或时间戳来区分,这篇文章主要介绍了java MultipartFile multipartFile文件上传重命名的相关资料,需要的朋友可以参考下2024-09-09springboot整合springsecurity与mybatis-plus的简单实现
Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发。因此作为spring全家桶中的Spring Security在java领域很常用2021-10-10RestTemplate设置超时时间及返回状态码非200处理
这篇文章主要为大家介绍了RestTemplate设置超时时间及返回状态码非200处理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06
最新评论