springIOC principle underlying implementation

By Dom4j + java reflector
         1. Analytical xml
         2. Find beanid xml node using the corresponding node attributes acquired class
         3. initialize the java class reflection mechanism
         4. java reflection mechanism to the private property assignment





import java.lang.reflect.Field;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ClassPathXmlApplicationContext {

    private static String Path;
    private static String ID;
    private static String CLASS;
    private static String NAME;
    private static String VALUE;
    public void init(){
        ID="id";
        CLASS="class";
        NAME="name";
        VALUE="value";
    }
    the ClassPathXmlApplicationContext public (String path) {
        the init ();
        // Get the name of the file to read spring
        this.Path = path;
    }
    public Object the getBean (String the beanId) throws DocumentException, a ClassNotFoundException, an InstantiationException is, IllegalAccessException, a NoSuchFieldException, a SecurityException {
        //. 1. parse XML
        IF (StringUtils.isEmpty (the beanId)) {
            return null;
        }
        SAXReader SAXReader new new SAXReader = ();
        the Document Read = saxReader.read (.. this.getClass () getClassLoader () the getResource (the Path));
        // get root to
        the Element read.getRootElement the rootElement = ();
        // get all two nodes
        List <the Element> Elements rootElement.elements = ();
        for (the Element Element: Elements) {
            String ID = element.attributeValue (ID);
            IF {(beanId.equals (ID)!)
                // end of the cycle
                Continue;
            }
            // Find beanid xml node 2 using the corresponding node acquires attribute class.
            // Get a bean from the configuration file
            String attClass = element.attributeValue (the cLASS);
            // java. 3 using the anti-color class initialization mechanism
            <?> class forName the Class.forName = (attClass);
            Object = forName.newInstance the newInstance ();
            // get property. 4.
            List <the Element> sonEle element.elements = ();
            for (the Element EL: sonEle) {
                String attrName = el.attributeValue(NAME);
                String attrValue = el.attributeValue(VALUE);
                //获取到私有属性
                Field declaredField = forName.getDeclaredField(attrName);
                declaredField.setAccessible(true);
                declaredField.set(newInstance, attrValue);
            }
            return newInstance;
        }
        
        return null;
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, DocumentException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserEntity user = (UserEntity) classPathXmlApplicationContext.getBean("user1");
        System.out.println(user.toString()+"\t"+user.getUserName()+"\t");
    }
}


Guess you like

Origin blog.csdn.net/jxz999000/article/details/80384604