Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to discuss the practical Application of Java proxy Mode and reflection Mechanism

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to explore the practical application of Java proxy mode and reflection mechanism, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Java provides a mechanism to dynamically execute methods and constructors, as well as array operations, and so on. This mechanism is called reflection. The proxy pattern is to provide a proxy for other objects to control access to this object, so that our target class and proxy class implement the same interface and call the method of the target class object in the proxy class.

Reflection mechanism is the basis for the implementation of many popular Java frameworks, including Spring, Hibernate and so on. If we add the reflection mechanism to the proxy mode of Java, we can implement a common proxy class, which saves us a lot of effort.

Import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; / * method proxy class * @ author rongxinhua * * / public class MethodProxy {private Class clazz; / / the class to which the object belongs private Object target; / / target object private Method method; / / target method private Object [] params / / Parameter array @ SuppressWarnings ("unchecked") public MethodProxy (Object target, String methodName, Object... Params) {rebindTarget (target, methodName, params); / / set target object and method} / * reset target object and method * @ param target * @ param methodName * @ param params * / public void rebindTarget (Object target, String methodName, Object... Params) {this.target = target; this.clazz = target.getClass (); rebindMethod (methodName, params) / / set target method} / * reset target method * @ param methodName * @ param params * / public void rebindMethod (String methodName, Object... params) {this.params = params; int paramLength = params.length; Class [] paramTypes = new Class [paramLength] For (int I = 0; I < paramLength; I + +) {paramTypes [I] = params.getClass ();} try {this.method = clazz.getMethod (methodName, paramTypes);} catch (SecurityException e) {e.printStackTrace () } catch (NoSuchMethodException e) {e.printStackTrace ();}} / * dynamically call the bound method * / public void doMethod () {try {this.method.invoke (target, params) } catch (IllegalArgumentException e) {e.printStackTrace ();} catch (IllegalAccessException e) {e.printStackTrace ();} catch (InvocationTargetException e) {e.printStackTrace ();} import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method / * * method proxy class * @ author rongxinhua * / public class MethodProxy {class private Object target; / / target object private Method method; / / target method private Object [] params; / / parameter array @ SuppressWarnings ("unchecked") public MethodProxy (Object target, String methodName, Object... Params) {rebindTarget (target, methodName, params); / / set target object and method} / * reset target object and method * @ param target * @ param methodName * @ param params * / public void rebindTarget (Object target, String methodName, Object... Params) {this.target = target; this.clazz = target.getClass (); rebindMethod (methodName, params); / / set target method} / * reset target method * @ param methodName * @ param params * / public void rebindMethod (String methodName, Object... params) {this.params = params; int paramLength = params.length; Class [] paramTypes = new Class [paramLength] For (int I = 0; I < paramLength; I + +) {paramTypes [I] = params[ I] .getClass ();} try {this.method = clazz.getMethod (methodName, paramTypes);} catch (SecurityException e) {e.printStackTrace ();} catch (NoSuchMethodException e) {e.printStackTrace () }} / * dynamically call bound method * / public void doMethod () {try {this.method.invoke (target, params);} catch (IllegalArgumentException e) {e.printStackTrace ();} catch (IllegalAccessException e) {e.printStackTrace ();} catch (InvocationTargetException e) {e.printStackTrace () } in this way, you can call a method of an object dynamically, and write a test code as follows: public class Manager {public void say () {System.out.println ("Nobody say nothing");} public void love (String boy, String girl) {System.out.println (boy + "love" + girl) } public class Manager {public void say () {System.out.println ("Nobody say nothing");} public void love (String boy, String girl) {System.out.println (boy + "love" + girl);}}

We call the say () and love () methods in the Manager class through the proxy class, and the test code is as follows:

Manager man = new Manager (); / / Target object MethodProxy proxy = new MethodProxy (man, "say"); / / method proxy object proxy.doMethod (); / / call proxied method proxy.rebindMethod ("love", "Tom", "Marry"); / / rebind method proxy.doMethod (); / / call proxied method Manager man = new Manager () / / Target object MethodProxy proxy = new MethodProxy (man, "say"); / / method proxy object proxy.doMethod (); / / call proxied method proxy.rebindMethod ("love", "Tom", "Marry"); / / rebind method proxy.doMethod (); / / call proxied method

In this way, the method of the dynamic proxy calling the object is implemented, and the output of the above code is not posted. If you want to set up functions such as pre-notification and post-notification, it is also easy to implement, as long as you set it before and after the "proxy.doMethod ()" code. Extended application: we added the following methods to the above MethodProxy class:

/ * get comments on method * @ param anClazz comment class * @ return * / public Annotation getAnnotation (Class anClazz) {return this.method.getAnnotation (anClazz);} / * get note on method * @ param anClazz comment class * @ return * / public Annotation getAnnotation (Class anClazz) {return this.method.getAnnotation (anClazz);}

What is the use of this method to read the Annotation on the method? Let's write a note to test it.

@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ interface Low {int boyAge (); int girlAge ();} @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ interface Low {int boyAge (); int girlAge ();}

We are going to introduce Annotation-related classes:

Import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target

Let's write another business class for testing:

Public class LoveManager {@ Low (boyAge=12, girlAge=10) public void beAbleToLove (Person boy, Person girl) {System.out.println (boy.getName () + "is able to love" + girl.getName ());}} public class Person {private String name; private int age Public Person (String name, int age) {this.name = name; this.age = age;} / / getter method slightly} public class LoveManager {@ Low (boyAge=12, girlAge=10) public void beAbleToLove (Person boy, Person girl) {System.out.println (boy.getName () + "is able to love" + girl.getName ()) }} public class Person {private String name; private int age; public Person (String name, int age) {this.name = name; this.age = age;} / / getter method slightly}

Write the proxy object test code in the previous example:

LoveManager loveManager = new LoveManager (); Person boy = new Person ("Tom", 13); Person girl = new Person ("Marry", 10); proxy.rebindTarget (loveManager, "beAbleToLove", boy, girl); / / rebind objects and methods Low low = (Low) proxy.getAnnotation (Low.class) If (boy.getAge () < low.boyAge ()) {System.out.println (boy.getName () + "under legal age, you can't fall in love!") ;} else if (girl.getAge () < low.girlAge ()) {System.out.println (girl.getName () + "under legal age, you can't fall in love!") ;} else {proxy.doMethod ();} LoveManager loveManager = new LoveManager (); Person boy = new Person ("Tom", 13); Person girl = new Person ("Marry", 10); proxy.rebindTarget (loveManager, "beAbleToLove", boy, girl); / / rebind objects and methods Low low = (Low) proxy.getAnnotation (Low.class) If (boy.getAge () < low.boyAge ()) {System.out.println (boy.getName () + "under legal age, you can't fall in love!") ;} else if (girl.getAge () < low.girlAge ()) {System.out.println (girl.getName () + "under legal age, you can't fall in love!") ;} else {proxy.doMethod ();}

This is realized, through the reflection mechanism of Java to read the value of Annotation, and according to the value of Annotation, to deal with the judgment of the validity of business data, or to dynamically inject objects facing the aspect, or to be a log, interceptor, and so on. This usage is often seen in many frameworks, and we might as well adopt it when developing our own Java components.

After reading the above, have you mastered how to explore the practical application of Java proxy mode and reflection mechanism? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report