Note: This is a simple example of learning to understand the Spring IOC container for !!!!
1, IOC container implemented process:
- Load XML configuration files, and traverse the label file
- Acquiring a single profile ID and Bean Class property, and then load the corresponding class according to Class file and create an instance of Bean
- Getting a property value in the Properties tab, filled in Bean
- We will set a good Bean registered with the IOC container
2, code shows:
package com.baozi.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.FileInputStream; import java.io.InputStream; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * @author BaoZi * @create 2019-07-19-8:54 */ public class SimpleIOC { private Map<String, Object> beanMap = new HashMap<>(); public SimpleIOC(String location) throws Exception { loadBeans(location); } * this method parses the default installation according to the local platform , automatic creation of a factory object and returns. Object the getBean public (String name) { Object beanMap.get the bean = (name); IF (the bean == null) { the throw new new an IllegalArgumentException ( "name with the bean there IS NO" + name); } return the bean; } Private void loadBeans (String LOCATION) throws Exception { // load the xml configuration file // parse the XML document to be converted into an input stream object so that it is a DOM parser parsing the inputStream = new new inputStream the FileInputStream (LOCATION); / ** * the javax.xml. parser object parsers used to create the package DocumentBuilderFactory DOM mode, * DocumentBuilderFactory is an abstract factory class, it can not be directly instantiated, but the class provides a method newInstance, * / // Call DocumentBuilderFactory.newInstance () method to get create a DOM parser factory. Factory = DocumentBuilderFactory.newInstance the DocumentBuilderFactory (); // call to obtain a factory object methods newDocumentBuilder DOM parser object. DocBuilder = factory.newDocumentBuilder DocumentBuilder (); // call parse DOM parser object () method to parse XML documents, obtained on behalf of the entire document Document object, // can be operated for the entire XML document using DOM properties. DOC = docBuilder.parse the Document (inputStream); // get the root node of the XML document the Element doc.getDocumentElement the root = (); // get node subnode a NodeList root.getChildNodes Nodes = (); // iterate <bean> tag for (int i = 0; i <nodes.getLength (); i ++) { Node = nodes.item the Node (I); IF (the instanceof the Element Node) { ELE = the Element (the Element) Node; String ID = ele.getAttribute ( "ID"); String className = ele.getAttribute ( "class"); // loading beanClass Class beanClass = null; the try { beanClass = the Class.forName (className ); } the catch (a ClassNotFoundException E) { e.printStackTrace (); return; } // Create the bean object beanClass.newInstance the bean = (); // 遍历 <property> 标签 // Document object using the getElementsByTagName () method, we can get a NodeList object, // a Node object represents a tag element in an XML document, and NodeList object and watch his name and knowing Italian, // represents is a list of the Node objects: a NodeList propertyNodes = ele.getElementsByTagName ( "Property"); for (int J = 0; J <propertyNodes.getLength (); J ++) { Node propertyNode propertyNodes.item = (J); IF ( the instanceof the Element propertyNode) { the Element propertyElement = (the Element) propertyNode; String name = propertyElement.getAttribute ( "name"); String value = propertyElement.getAttribute ( "value"); // use the reflection to access the relevant field bean accessible the throw new new an IllegalArgumentException ( "REF config error"); DeclaredField = bean.getClass Field, () getDeclaredField (name);. DeclaredField.setAccessible (to true); IF (! = Null && value.length value ()> 0) { attribute value // is filled into the associated field declaredField.set (the bean, value); } the else { // this is a circular reference problem Bean object attribute (to no treatment) String propertyElement.getAttribute REF = ( "REF"); IF (REF == null || ref.length ( ) == 0) { } // references to populate the field declaredField.set (bean, getBean (ref) ); } // Register the bean to bean container registerBean (ID, bean); } } } } } // here Bean object imitating implantation process vessel IOC Private void registerBean (ID String, Object bean) { beanMap.put ( ID, the bean); } }
3, test case:
Bean strength corresponding class:
public class Car { private String name; private String length; private String width; private String height; private Wheel wheel; //.......................... } public class Wheel { private String brand; private String specification ; // ............................ }
Test XML document used:
<beans> <bean id="wheel" class="com.baozi.Wheel"> <property name="brand" value="Michelin" /> <property name="specification" value="265/60 R18" /> </bean> <bean id="car" class="com.baozi.Car"> <property name="name" value="Mercedes Benz G 500"/> <property name="length" value="4717mm"/> <property name="width" value="1855mm"/> <property name="height" value="1949mm"/> <property name="wheel" ref="wheel"/> </bean> </beans>
Test categories:
public class SimpleIOCTest { @Test public void getBean() throws Exception { String location = SimpleIOC.class.getClassLoader().getResource("spring-test.xml").getFile(); SimpleIOC bf = new SimpleIOC(location); Wheel wheel = (Wheel) bf.getBean("wheel"); System.out.println(wheel); Car car = (Car) bf.getBean("car"); System.out.println(car); } }