java 动态代理实践aop -尊龙游戏旗舰厅官网
2019独角兽企业重金招聘python工程师标准>>>
大家都知道spring中aop是通过java动态代理实现的,今天就来简单学习下demo。
java动态代理主要有两个核心类,invocationhandler和proxy。
/*** {@code invocationhandler} is the interface implemented by* the invocation handler of a proxy instance.**each proxy instance has an associated invocation handler.* when a method is invoked on a proxy instance, the method* invocation is encoded and dispatched to the {@code invoke}* method of its invocation handler.** @author peter jones* @see proxy* @since 1.3*/ public interface invocationhandler
所有的handler类要实现invocationhandler接口,并关联到proxy实例上,最后会分发到invocationhandler的invoke方法上。
/*** {@code proxy} provides static methods for creating dynamic proxy* classes and instances, and it is also the superclass of all* dynamic proxy classes created by those methods.**to create a proxy for some interface {@code foo}:*
* invocationhandler handler = new myinvocationhandler(...);* class proxyclass = proxy.getproxyclass(* foo.class.getclassloader(), new class[] { foo.class });* foo f = (foo) proxyclass.* getconstructor(new class[] { invocationhandler.class }).* newinstance(new object[] { handler });** or more simply:*
* foo f = (foo) proxy.newproxyinstance(foo.class.getclassloader(),* new class[] { foo.class },* handler);*******************************/ public class proxy implements java.io.serializable
通过该类的静态方法创建要动态代理的类。
下面看下demo
1. 先创建一个接口
public interface targetinterface {int targetmethod(int num); }2. 实例化该接口
public class targetclass implements targetinterface {@overridepublic int targetmethod(int number) {system.out.println("调用目标类的方法targetmethod..."); return number; } }3. 创建代理处理类,invocationhandler子类
public class proxyhandler implements invocationhandler {object concreteclass;public proxyhandler(object concreteclass) {this.concreteclass = concreteclass;}@overridepublic object invoke(object proxy, method method, object[] args)throws throwable {system.out.println("proxy:" proxy.getclass().getname()); system.out.println("method:" method.getname()); system.out.println("args:" args[0].getclass().getname()); system.out.println("before invoke method..."); object object = method.invoke(concreteclass, args);system.out.println("after invoke method..."); return object; } } proxy: 指代我们所代理的那个真实对象 method: 指代的是我们所要调用真实对象的某个方法的method对象 args: 指代的是调用真实对象某个方法时接受的参数 public class example {public static void main(string[] args) {targetclass cc = new targetclass();invocationhandler ih = new proxyhandler(cc);targetinterface tf = (targetinterface) proxy.newproxyinstance(cc.getclass().getclassloader(), cc.getclass().getinterfaces(), ih);int i = tf.targetmethod(5);} } public static object newproxyinstance(classloader loader, class[] interfaces, invocationhandler h) throws illegalargumentexceptionloader: 一个classloader对象,定义了由哪个classloader对象来对生成的代理对象进行加载 interfaces: 一个interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了 h: 一个invocationhandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个invocationhandler对象上注意:通过 proxy.newproxyinstance 创建的代理对象是在jvm运行时动态生成的一个对象,它并不是我们的invocationhandler类型,也不是我们定义的那组接口的类型,而是在运行是动态生成的一个对象,并且命名方式都是这样的形式,以$开头,proxy为中,最后一个数字表示对象的标号。
动态代理有个缺陷,就是创建时需要参数interfaces,即被代理的类,需要实现该接口。
转载于:https://my.oschina.net/android520/blog/700945
总结
以上是尊龙游戏旗舰厅官网为你收集整理的java 动态代理实践aop的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: android中webview加载本地h
- 下一篇: jquery lightbox图片放大预