In order to deepen understanding spring has written itself today a simulated spring ....
Steps:
1. Using Jdom to parse Bean.xml
2. Create Classpathxmlapplicaitoncontext to simulate IOC
3. Parse all the <bean/> first, and then parse all the <property/> if edge parsing <bean/>, Edge parsing <property/> Will cause the property's ref to not find the corresponding bean.
4. Use simple reflection to achieve IOC.
Directory structure:
Here only the core code, the rest of the bean,dao,service, not important, not given. Interested comrades can click here to download the source code.
Classpathxmlapplicationcontext:
Package Glut.spring;import Java.io.inputstream;import Java.lang.reflect.method;import java.util.HashMap;import Java.util.list;import java.util.map;import java.util.map.entry;import Org.jdom.document;import org.jdom.Element; Import Org.jdom.input.saxbuilder;public class Classpathxmlapplicationcontext {/** * for storing <bean/> */private Map <string, object> beans = new hashmap<> ();/** * for storage <property/> */private map<string, list< element>> properties = new hashmap<> ();/** * Converts an XML file into an input stream, passing in as a parameter. * @param is * @throws Exception */public classpathxmlapplicationcontext (InputStream is) throws Exception {//TODO Auto-gen Erated Constructor stubautowired (is);} /** * Analog di * @param is * @throws Exception */private void autowired (InputStream is) throws Exception {Saxbuilder sb = new Saxbuilder ();D ocument doc = Sb.build (IS); Element rootelement = Doc.getrootelement (); list<element> Elementofbeans = Rootelement.getchildren ("Bean");//Traverse all the <bean/>for in the XML (Element E:elementofbeans) {String Beanid = E.getattributevalue ("id"); String Beanclz = E.getattributevalue ("class"), Object beaninstance = Class.forName (BEANCLZ). newinstance ();// Beanid and Bean instances are stored in mapbeans.put (Beanid, beaninstance);//Store all the property first, and then initialize the property after the bean initializes. Doing so may cause some property to fail to initialize Properties.put (Beanid, E.getchildren ("property"));} Dependency injection Simulationfor (entry<string, list<element>> entry:properties.entrySet ()) {for ( Element E:entry.getvalue ()) {String propertyname = E.getattributevalue ("name"); String propertyref = E.getattributevalue ("ref");//inject string methodName = "Set" + propertyname.substring (0, 1) through the Set method. toUpperCase () + propertyname.substring (1);//Beanobject beaninstance = Beans.get (Entry.getkey ()) via Beanid;// The value of ref is used to find the corresponding bean, and if there is no corresponding bean, an exception will be thrown when using getclass below. Object refbeaninstance = Beans.get (propertyref); Method Settermethod = Beaninstance.getclass (). GetMethod (methodname,//Oh, the function is a bit humble, Only the first interface implemented by the Refbean is supported by default. Refbeaninstance.getclass (). Getinterfaces () [0]);//Call the corresponding setter method to inject the bean of ref into the specified bean. Settermethod.invoke (beaninstance, refbeaninstance);}} /** * Based on beanname get Bean */public Object getbean (String beanname) {//TODO auto-generated method Stubreturn Beans.get (Beanna me);}}
Test code:
Package Glut.test;import Glut.bean.user;import Glut.service.userservice;import Glut.spring.classpathxmlapplicationcontext;import Org.junit.test;public class Springtest {@Testpublic void Test () Throws Exception {Classpathxmlapplicationcontext ctx = new Classpathxmlapplicationcontext (This.getclass (). getClassLoader (). getResourceAsStream ("Beans.xml")); UserService UserService = (userservice) ctx.getbean ("UserService"); User user = New User ("User", "123"); Userservice.add (user);}}
The result of printing is the ToString of User:
User [Uid=user, pwd=123]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simulation for spring IOC functionality