#反射
1. is the Java API, is the Java provides the ready-made class!!
--accept the functionality provided by the api!
2. is a dynamic execution mechanism provided by java, dynamically loading classes, dynamically creating objects, dynamically accessing properties, and dynamically invoking METHODS.
# #反射用途
1. The structure of the parsing class in Eclipse uses reflection
2.JUnit recognition tested method uses reflection
--JUNIT3 uses reflection to find the method at the beginning of test
--JUNIT4 uses the reflection analytic @test to find the test method
3.Spring manages bean objects, injecting bean properties using reflection
--create a Bean object instance with reflection
--injecting the Bean's properties with reflection
4. The interpretation of annotations uses the reflection
--support annotations with the reflection API
5. Enforcing private methods (accessing private properties)
Analysis Diagram:
class--package: main/springstru, main/resources
------------------------------split line-----------------------
Springstru/applicationcontext.java
packagespringstru;Importjava.io.InputStream;Importjava.util.HashMap;Importjava.util.List;Importjava.util.Map;Importorg.dom4j.Document;Importorg.dom4j.Element;Importorg.dom4j.io.SAXReader; public classApplicationContext {//is the Bean object that caches the spring container Privatemap<string,object> beans =Newhashmap<string, object>(); /*** Initialize the current container with a configuration file * Initialize all Bean objects with an XML configuration file*/ publicApplicationContext (String XML)throwsexception{//use dom4j to read XML files//parse the contents of the XML file to get the Bean's class name and Bean id://dynamically load classes and create objects based on the class name//add an object and the corresponding ID to the map//read stream from resource (classpath)InputStream in =getclass (). getclassloader (). getresourceasstream (xml); Saxreader Reader=NewSaxreader (); Document Doc=Reader.read (in); In.close (); //Parse Xml: <beans><bean><bean><bean><bean>Element root =doc.getrootelement (); //reads all the bean child elements in the root elementlist<element> list = Root.elements ("bean"); for(Element E:list) {//e is the bean element ID attribute and the class attributeString id = e.attributevalue ("id"); String ClassName= E.attributevalue ("class"); //dynamically loading classes, creating objects dynamicallyClass CLS =Class.forName (className); Object Bean=cls.newinstance (); Beans.put (id,bean); } } publicObject Getbean (String id) {//finds an object in map based on ID and returns the object returnBeans.get (id); } //using generic methods: The advantage is that you can reduce the one-time type conversion public<T> T Getbean (String id,class<t>Cls) { return(T) Beans.get (id); }}
------------------------------split line-----------------------
Springstru/demo.java
package springstru; public class Demo { public static void main (string[] Args) throws exception{applicationcontext ctx = new Applicationco ntext ("spring-context.xml" = (foo) ctx.getbean ("foo" = Ctx.getbean ("foo", foo.
------------------------------split line-----------------------
/resources/spring-context.xml
<Beans> <!--Configuring Bean Elements - <BeanID= "foo"class= "springstru." Foo "></Bean> <BeanID= "date"class= "java.util.Date"></Bean> <BeanID= "testCase"class= "demo." TestCase "></Bean></Beans>
Reflection-spring manages beans, injecting the reflection mechanism of bean properties.