Implement the Spring IOC Framework Yourself (super verbose)

Source: Internet
Author: User
Tags xpath

1.IOC Introduction:

Control inversion is the IOC (inversion of Control), which is that the application itself is not responsible for the creation of dependent objects and the maintenance of relationships between objects, but is the responsibility of the external container. Thus control is transferred from the application to the external container, and the transfer of control is called reversal.

The IOC is a big concept that can be implemented in different ways. For example,:<1> relies on lookup (Dependency lookup): The container provides callback interfaces and context to the component. Both EJB and Apache Avalon Use this approach. <2> Dependency Injection (Dependency injection): The component does not do a location query, only provides a common Java method for the container to determine the dependency relationship. The latter is the most popular type of IOC nowadays.

2. Implement IOC Yourself

Overall framework:

First you need to read the configuration file: Note that there is no schema for the configuration file.

  1  <?xml version=" 1.0 "encoding=" UTF-8 "?>  <beans> 3  <!--Configure A to the configuration file--& Gt  <bean id=" A "class  =" Cn.itcast.bean.a "> 5  <!--The properties of configuration A, spring automatically injects the property value of a--> 6  <property name= "name" value= "Tom"/> 7  </bean> 8  <bean id= "B" class  = "cn.itcast.bean.b" > 9  <!--ref to say bean a injected-->10  <property name= "a" ref = "A" ></property>11  </bean>12  </beans> 

Java is an object-oriented language, and we need Java beans to encapsulate this information when reading a configuration file.

1  Public classBean {2     /**3 * Package4 * <bean id= "A" class= "Cn.itcast.bean.a" >5      * .... A number of <property>6 * </bean>7      */8     PrivateString ID;9     Ten     PrivateString ClassName; One      A     Privatelist<property> Properties =NewArraylist<property>(); -      -     //setter.getter ... the  - @Override -      PublicString toString () { -         return"Bean [id=" + ID + ", classname=" + ClassName + ", properties=" ++ Properties + "]"; -     } +      A}
1  Public classProperty {2     /**3 * Package <property>4 * <property name= "name" value= "Tom"/>5 * <property name= "A" ref= "a" ></property>6      */7     PrivateString name;8     9     PrivateString value;Ten      One     PrivateString ref; A      -     //setter.getter ... - @Override the      PublicString toString () { -         return"Property [Name=] + name +", value= "+ Value +", ref= "+ref -+ "]"; -     } +      -  +}

At this point, the class used to encapsulate the information in the configuration file has been written. The following is the need to create a class that parses an XML file

1  Public classConfigManager {2     3     //reads the configuration file and returns the read result. 4      Public StaticMap<string,bean>getconfig (String path) {5         //Create a Map object for return6map<string,bean> map =NewHashmap<string,bean>();7         //DOM4J Implementation8             //1. Create a parser9Saxreader reader =NewSaxreader ();Ten             //2, load config file =>document element OneInputStream is = ConfigManager.class. getResourceAsStream (path); ADocument doc =NULL; -             Try { -Doc =Reader.read (IS); the}Catch(documentexception e) { - e.printstacktrace (); -                 Throw NewRuntimeException ("Please check that your XML configuration file is correct!") "); -             } +             //3. Define an XPath expression to get all the bean elements () -             //Find bean elements from an entire document +String XPath = "//bean"; A             //4. Traverse the bean element atlist<element> list =Doc.selectnodes (XPath); -             if(list!=NULL&&list.size () >0){ -                  for(Element beanele:list) { -Bean Bean =NewBean (); -                     //encapsulates the Id/class attribute of a bean element into a bean object -String id = beanele.attributevalue ("id"); inString className = Beanele.attributevalue ("Class"); - Bean.setid (ID); to Bean.setclassname (className); +                     //gets all the property sub-elements under the bean element, encapsulating the attribute Name/value/ref in the Properties object.  -list<element> children = beanele.elements ("Property"); the                     if(children!=NULL){ *                          for(Element child:children) { $Property Prop =NewProperty ();Panax NotoginsengString pName = child.attributevalue ("name"); -String PValue = Child.attributevalue ("Value"); theString PRef = Child.attributevalue ("ref"); + Prop.setname (pName); A Prop.setvalue (pValue); the Prop.setref (PREF); +                             //encapsulates a Property object into a Bean object - bean.getproperties (). Add (prop); $                         } $                     } -                     //encapsulates a bean object into a map (the map returned by the user) - map.put (ID, bean); the                 } -             }Wuyi             //5. Return to map results the             returnmap; -     } Wu}

Implement the Spring IOC Framework Yourself (super verbose)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.