The IOC (inverse of control) can be translated as "controlling reversal," but most people are accustomed to calling it "dependency injection". In spring, the IOC can configure the implementation class, parameter information, and so on in its corresponding configuration file, so that if you need to change the implementation class or parameter information, you only need to modify the configuration file, which further reduces the coupling between classes and classes on the basis of the above example. We can also inject other objects that are needed for an object, which is done in the configuration file, the spring IOC implementation is based on the Java reflection Mechanism, spring also acts as a factory role, we do not need to build our own factory class. The Spring factory class helps us to read the configuration file, inject the object with the reflection mechanism, and we can get the corresponding object through the bean's name. Let's take a look at the following mock Spring Bean factory class: Package org.amigo.reflection; Import Java.io.InputStream; Import Java.lang.reflect.Method; Import Java.util.HashMap; Import Java.util.Iterator; Import Java.util.Map; Import Org.dom4j.Attribute; Import org.dom4j.Document; Import org.dom4j.Element; Import Org.dom4j.io.SAXReader; /** * Bean factory class. * public class Beanfactory { private map<string, object> Beanmap = new hashmap<string, object> (); /** * The initialization of the Bean factory. * @param XML XML configuration file * * & nbsp; public void init (String xml) { try { /Read the specified configuration file Saxreader reader = new Saxreader (); ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader (); //Get the specified XML file from the class directory InputStream ins = Classloader.getresourceasstream (XML); Document doc = reader.read (INS); Element root = doc.getrootelement (); Element foo; //Traversing Bean for (Iterator i = root.elementiterator ("Bean"); I.hasnext ();) {   Foo = (Element) i.next (); //Get the Bean's property ID and Class Attribute id = foo.attribute ("id"); Attribute CLS = Foo.attribute ("class"); // Using the Java reflection mechanism to get class objects through the name of Class Class bean = Class.forName (Cls.gettext ()); // Get the information for the corresponding class Java.beans.BeanInfo info = Java.beans.Introspector.getBeanInfo (Bean); //Get its attribute description Java.beans.PropertyDescriptor pd[] = info.getpropertydescriptors (); //Setting Value method method mset = null; //Create an object Object obj = Bean.newinstance (); //traverse the Bean's property properties for (Iterator ite = Foo.elementiterator ("property"); Ite.hasnext ();) { Element Foo2 = (Element) Ite.next (); // Gets the property's name attribute &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; Attribute name = Foo2.attribute ("name"); String value = Null //Get the value of the child element value of the property for (Iterator ite1 = Foo2.elementIterator (" Value "); Ite1.hasnext ();) { element node = (element) Ite1.next (); value = Node.gettext (); break; } for (int k = 0; k < pd.length; k++) { & nbsp; if (Pd[k].getname (). Equalsignorecase (Name.gettext ()) { mset = pd[k]. Getwritemethod (); //Using Java reflection to maximize the invocation of a set method of an object and set the value in mset.invoke (obj, value); } } } //Placing objects in Beanmap, where key is ID value, value is Object Beanmap.put (Id.gettext (), obj); } } catch (Exception e) { System.out.println (e.tostring ()); } } /** * Gets the Bean's object through the bean's ID. * @param beanname Bean ID * @return Returns the corresponding object /* public object Getbean (String Beanname) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp Object obj = Beanmap.get (beanname); return obj; } / * * * test methods.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.