The handwritten spring instantiation bean source code only implements the Object getBean(String beanId) method.
That is to achieve:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xxx"); Object o = applicationContext.getBean("xxx");
//Define ApplicationContext interface: public interface ApplicationContext {<!-- --> Object getBean(String name); }
// ClassPathXmlApplicationContext class /** * Parse xml files, read tags, and instantiate objects * @param springPath spring.xml path */ import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ClassPathXmlApplicationContext implements ApplicationContext{<!-- --> private final Map<String, Object> beanObjects = new HashMap<>(); public ClassPathXmlApplicationContext(String springPath) {<!-- --> // Parse the spring configuration file, then instantiate the Bean and put the Bean into beanObjects try {<!-- --> // Parse xml file by path DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // Get the spring configuration file under the classpath InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(springPath); Document document = db.parse(in); // Get all Bean tags NodeList beans = document.getElementsByTagName("bean"); //System.out.println(beans.getLength()); for (int i = 0; i < beans.getLength(); i + + ) {<!-- --> // Get the name and value of all attributes of a certain bean tag and store them in the map collection NamedNodeMap attributes = beans.item(i).getAttributes(); // Get the id in the bean tag String beanId = attributes.getNamedItem("id").getNodeValue(); // Get the class in the bean tag String classValue = attributes.getNamedItem("class").getNodeValue(); //System.out.println(beanId); //System.out.println(classValue); // Create objects through reflection mechanism and expose them in advance Class<?> beanClazz = Class.forName(classValue); //Create object through constructor method Object beanObject = beanClazz.getConstructor().newInstance(); // Expose the bean beanObjects.put(beanId, beanObject); // System.out.println(beanObject); } //Traverse all bean tags again and assign values to properties for (int i = 0; i < beans.getLength(); i + + ) {<!-- --> // Get the name and value of all attributes of a certain bean tag and store them in the map collection NamedNodeMap attributes = beans.item(i).getAttributes(); // Get the id in the bean tag String beanId = attributes.getNamedItem("id").getNodeValue(); // Get the class in the bean tag String classValue = attributes.getNamedItem("class").getNodeValue(); // Get all property tags under the bean tag. Note that this contains a carriage return and needs to be filtered. NodeList childNodes = beans.item(i).getChildNodes(); List<Node> propertyNodes = new ArrayList<>(); for(int j=0; j<childNodes.getLength(); j + + ){<!-- --> if (childNodes.item(j).getAttributes() !=null) {<!-- --> propertyNodes.add(childNodes.item(j)); } // If it is a carriage return, this will be null //System.out.println(childNodes.item(j).getAttributes()); } // Get the name and value attributes of the property attribute or the name and ref attributes if (propertyNodes.size() != 0) {<!-- --> propertyNodes.forEach(propertyNode -> {<!-- --> // Get the attribute name and attribute value map of the property tag NamedNodeMap propertyNodeAttributes = propertyNode.getAttributes(); // Get the name attribute value Node nameNode = propertyNodeAttributes.getNamedItem("name"); // Get the value attribute value Node valueNode = propertyNodeAttributes.getNamedItem("value"); // Get the ref attribute Node refNode = propertyNodeAttributes.getNamedItem("ref"); //Assign a value to the object instantiated by the bean tag // Get the created object from beanObjects based on beanId Object beanObject = beanObjects.get(beanId); if (nameNode != null & amp; & amp; valueNode != null) {<!-- --> try {<!-- --> // Splicing set method String name = nameNode.getNodeValue(); String value = valueNode.getNodeValue(); String setMethod = "set" + name.toUpperCase().charAt(0) + name.substring(1); // Call the set method through reflection mechanism Class<?> clazz1 = Class.forName(classValue); Method method = clazz1.getDeclaredMethod(setMethod, String.class); method.invoke(beanObject, value); } catch (ClassNotFoundException e) {<!-- --> e.printStackTrace(); } catch (NoSuchMethodException e) {<!-- --> e.printStackTrace(); } catch (InvocationTargetException e) {<!-- --> e.printStackTrace(); } catch (IllegalAccessException e) {<!-- --> e.printStackTrace(); } } if (nameNode != null & amp; & amp; refNode != null){<!-- --> try {<!-- --> // Splicing set method String name = nameNode.getNodeValue(); String ref = refNode.getNodeValue(); String setMethod = "set" + name.toUpperCase().charAt(0) + name.substring(1); //System.out.println(setMethod); // Find the corresponding beanObject based on ref Object refObject = beanObjects.get(ref); //System.out.println(refObject.getClass().getName()); // Call method using reflection mechanism Class.forName(classValue).getDeclaredMethod(setMethod, refObject.getClass()).invoke(beanObject, refObject); } catch (IllegalAccessException e) {<!-- --> e.printStackTrace(); } catch (InvocationTargetException e) {<!-- --> e.printStackTrace(); } catch (NoSuchMethodException e) {<!-- --> e.printStackTrace(); } catch (ClassNotFoundException e) {<!-- --> e.printStackTrace(); } } }); } } } catch (Exception e) {<!-- --> e.printStackTrace(); } } @Override public Object getBean(String name) {<!-- --> return beanObjects.get(name); } }