[Software Engineering] JAVA reflection technology

JAVA technology reflection

  Java reflection technology is widely applied, it can be arranged: the fully qualified name of the class, method and parameters to complete initialization of an object, and even some reflection method. This can greatly enhance Java configurable 'of the basic principles pring IoC is true, of course, Spring IoC code much more complicated.

  Java's reflection range of content including objects built, reflection methods, annotated, parameter interface.

Construct an object reflector

  Java allows to construct an object reflector configuration information, such ReflectServicelmpl.java

 

 

   Then reflected by the method to build it

 

 

 

  Here is the code to generate an object and then returns. Objective This line of code to the class loader is registered fully qualified name of a class ReflectServicelmpl then initialize the class by a method newInstance objects using reflection mode is also very simple.

  

 

 

 

  This is a reflection of the class generated a construction method without any parameters, it left a problem that if all the construction method of a class where there is at least one parameter, how to build it with reflection

 

  Here achieved before-containing construction parameters will not be reflected by way generating object

 

  To be loaded by the class loader forName. Then getConstructor method, it may be a plurality of parameters, as defined herein String.class, meaning there is only a method of constructing parameters of type String. The method of the same name can be ruled out by this method, and then this time the object newInstance method to generate just newInstance method can have a parameter "Joe Smith" only. Is equivalent to the actual object new ReflectServicelmpl2 ( "John Doe"), is used here to generate the reflection object only.

Reflection method

  

Guess you like

Origin www.cnblogs.com/yyyyfly1/p/12275411.html