Java静态代理和动态代理总结
程序员文章站
2024-03-07 12:21:45
静态代理
第一种实现(基于接口):
1》接口
public interface hello {
void say(string msg);
}
2...
静态代理
第一种实现(基于接口):
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静态代理和动态代理总结,希望对大家有所帮助