The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any object, can invoke its arbitrary methods and properties; This dynamic acquisition of information and the ability to dynamically invoke object methods is called the reflection mechanism of the Java language.
Java Reflection (emission) mechanism: "When a program is run, it is allowed to change the program structure or variable type, which is called Dynamic language." From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language. But Java has a very prominent dynamic-related mechanism: Reflection, in Java, refers to the classes that we can load, detect, and use completely unknown during compilation at runtime. In other words, a Java program can load a runtime to know the name of a class, learn its full construct ( but not including the methods definition ), and generate its object entities, or set values on its fields, or evoke its methods.
The Java Reflection mechanism provides the following functions: At run time, judge the class to which any object belongs, construct an object of any class at run time, and at run time, judge what any class has member Variables and methods, methods for invoking any object at run time, and generating dynamic proxies.
Use reflection to detect or modify the state or behavior of certain objects at run time. Allows the program to dynamically obtain the internal information of a class of any known name through the Reflecttion APIs at run time, including its modifiers (such as public static, and so on) the implementation of the parent class (such as the object) interface (for example, serializable). It also includes all the information about member variables and methods, and can change the contents of member variables at run time or invoke methods (including private methods, we know that the encapsulation mechanism of private adornments cannot be used externally, but through reflection, we can change private variables and methods). For example, the spring framework is a well-written framework, and his internal processing does not know what classes the user is going to write, because that is later determined by his people, so the user is using it to configure the classpath in the configuration file, which is a typical example of reflection.
This makes it possible to load, detect, and use classes that are completely unknown during compilation at run time.
Two classes involved: class and ClassLoader
ClassLoader will all kinds of load into memory, one class is an object, due to the dynamic loading mechanism, class is loaded, and is required to load. The static statement block is loaded only once, and the dynamic statement block is executed every time new object is newly, where the core class is loaded by bootstrap class loader. In this way, you can know the name of this class and be able to new out an object:
First load it out:
String s = "M";
Class C = class.forname (str); The return value is a class object, and the parameter is the name of the category;
the role of Class.forName () is to require the JVM to find and load the specified class, Which means JVM executes the static code of the class U
Dynamically load and create class objects, such as when you want to create objects based on a string entered by the user.
String str = "User-entered string";
Class t = class.forname (str);
T.newinstance ();
Object o = c.newinstance ();//New Object
method [] ms= c.getmethods (o);//Get all methods
Method M1; String S1 = M1.getname ();//Get Method name
M1.invoke (o);
Invoke: A method of variable parameters. Parameter change (Object Obj,object...args), which can be passed from 0 to several parameters later
Getparametertypes ()
The Java.lang.reflect.Method.getParameterTypes () method returns an array of class objects that represent the formal parameter types of the method represented by this method object in a declaration order. If the underlying method has no parameters, an array of length 0 is returned.
Method[] methods = SampleClass.class.getMethods ();
class[] parametertypes = Methods[1].getparametertypes ();
About the Spring framework:
1, Spring is a new Kaiyuan lightweight application Development framework, its purpose is to simplify enterprise-level application development, reduce intrusion;
2, Spring's IOC and AOP applications, the coupling degree of the component is minimized, namely decoupling, easy to maintain and upgrade the system;
3, can be integrated with the third-party framework and technology applications, can freely choose technology for development.
Annotations:
A) @Component for universal annotations
b) @Repository annotations for durable layer components
c) Annotations @Service for business layer components
d) Note @Controller for the control layer
e) @Scope for the bean's scope annotation
f) Annotations @Autowired to the dependency between the specified beans
g) @Inject annotations for the dependency between the specified beans
h) @Value the annotation that injects the value of the spring expression
j) @ResponseBody annotations for the Ajax return type
Highlight part is not understand the place, hope the big guy advice ... :):):)
Some of the information from the blog and Baidu Encyclopedia, invasion Delete:)
Java Reflection mechanism