Dependency injection (that is, how an object obtains a reference to the object he depends on, the reversal of this responsibility.) From the code inside this object, go to the container to assemble and manage the components.
Dependent access
Open closure principle (open-closed Principle)
Turning an entity from relying on another entity to relying on an interface (protocol-object Principle)
The general implementation of construction injection and attribute injection are implemented through reflection and XML (the construction method of the class to be created is obtained by reflection mechanism, similarly, property injection is assigned, created according to these in the DI window node by reflecting all the properties of the class to be created )
The following code is pasted from http://www.cnblogs.com/devinzhang/p/3862942.html
public class Main {public static void main (string[] args) {/******************** IOC control inversion and dependency injection ************* ////////using containers to inject attribute values directly through XML files, add only the required//Chinese and American in the main class, and when classes and methods are modified, the code does not have to be modified at all, only the XML file needs to be modified. Fully realized understanding of the decoupling beanfactory beanfactory = new Beanfactory (); Beanfactory.init ("/config.xml"); UserBean UserBean = (UserBean) beanfactory.getbean ("UserBean"); System.out.println ("username=" + userbean.getusername ()); System.out.println ("password=" + Userbean.getpassword ()); }}/******************** IOC control inversion and Dependency injection ***************************///the following is the spring IOC implementation: Bean factory class Beanfactory { Private map<string, object> beanmap = new hashmap<string, object> (); public void init (String fileName) {try {///read the specified profile Saxreader reader = new Saxread ER (); System.out.println (Xmlpath); String realpathstring = new File (""). Getcanonicalpath (); Document document = Reader.read (new File (realpathstring + "/src/com/devin/") + FileName); Element root = Document.getrootelement (); Element foo; Traverse Bean for (Iterator i = root.elementiterator ("Bean"); I.hasnext ();) {foo = (Element) i.next (); Gets the Bean's attribute ID and class Attribute id = foo.attribute ("id"); Attribute cls = Foo.attribute ("class"); Using the Java reflection mechanism, get class object class Bean = Class.forName (Cls.gettext ()) by class name; Get information about class Java.beans.BeanInfo info = Java.beans.Introspector.getBeanInfo (bean); Gets its property description java.beans.PropertyDescriptor pd[] = info.getpropertydescriptors (); To set the value of method mSet = null; Creates an object obj = Bean.newinstance (); Iterates over the property properties for the bean for (Iterator ite = Foo.elementiterator ("the Property"); Ite.hasnext () ;) {element Foo2 = (Element) Ite.next (); Gets the name attribute of the property Attribute name = Foo2.attribute ("name"); String value = null; Gets the value of the property's child element, 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++) {if (Pd[k].getname (). equals IgnoreCase (Name.gettext ())) {MSet = Pd[k].getwritemethod (); Mset.invoke (obj, value); }}}//Put the object in Beanmap, where key is the 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. Public Object Getbean (String beanname) {Object obj = Beanmap.get (beanname); return obj; }}userbean.javapublic class UserBean {private String userName; private String password; Public String GetPassword () {return password; } public String GetUserName () {return userName; } public void Setusername (String userName) {this.username = UserName; } public void SetPassword (String password) {this.password = password; }}config.xml<?xml version= "1.0" encoding= "UTF-8"?><beans> <bean id= "UserBean" class= " Com.devin.UserBean "> <property name=" userName "> <value> Zhang San </value> </property> <property name= "password" > <value>Jack</value> </pr Operty> </bean></beans>
Dependency Injection, control inversion