How does a spring container manage beans?
We simulate the internal implementation of the spring container:
(1) Read the bean configuration file in spring
Read Bean configuration file public void readXml (String beandir) {Document document = Null;try{saxreader SX = new Saxreader (); URL Xmlpath = This.getclass (). getClassLoader (). GetResource (beandir);d ocument = Sx.read (Xmlpath); Map<string,string>nsmap = new hashmap<string,string> (); Nsmap.put ("ns", "http://www.springframework.org /schema/beans "); XPath XP = Document.createxpath ("//ns:beans/ns:bean"); Xp.setnamespaceuris (nsmap);//Set namespace list<element> el = Xp.selectnodes (document);//Gets all nodes under the documents for (Element Element:el) {String id = element.attributevalue ("id"); String className = Element.attributevalue ("class"); Peoplebean Peoplebean = new Peoplebean (id,classname); List.add (Peoplebean);}} catch (Exception e) {e.printstacktrace ();}}
2. Instantiate beans
The complete code that contains the previous section is:
Package Junit.test;import Java.io.stringreader;import Java.net.url;import java.util.arraylist;import Java.util.hashmap;import java.util.list;import java.util.map;import Org.dom4j.document;import org.dom4j.Element; Import Org.dom4j.xpath;import Org.dom4j.io.saxreader;public class Learncreatebean {//bean array private list< Peoplebean>list = new arraylist<peoplebean> ();p rivate map<object,object>map = new HashMap<Object, Object> (); Learncreatebean (String beandir) {readXml (Beandir); Instancebeans ();} /** * Instantiate bean */public void Instancebeans () {for (Peoplebean bean:list) {try {map.put (), Bean.getid ( Bean.getclassname ()). newinstance ());} catch (Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();}}} Read Bean configuration file public void readXml (String beandir) {Document document = Null;try{saxreader SX = new Saxreader (); URL Xmlpath = This.getclass (). getClassLoader (). GetResource ("Bean.xml");d ocument = Sx.read (Xmlpath); Map<string,string>nsmap = new hashmap<string,string> (); Nsmap.put ("ns", "http://www.springframework.org /schema/beans "); XPath XP = Document.createxpath ("//ns:beans/ns:bean"); Xp.setnamespaceuris (nsmap);//Set namespace list<element> el = Xp.selectnodes (document);//Gets all nodes under the documents for (Element Element:el) {String id = element.attributevalue ("id"); String className = Element.attributevalue ("class"); Peoplebean Peoplebean = new Peoplebean (id,classname); List.add (Peoplebean);}} catch (Exception e) {e.printstacktrace ();}} Public Object Getbeanu (String beanname) {return this.map.get (beanname);}}
3. Testing
@Testpublic void Test () {//Instantiate spring container applicationcontext CTX = new Classpathxmlapplicationcontext ("Bean.xml"); Peopleservice Peopleservice = (peopleservice) ctx.getbean ("Peopleservice");p Eopleservice.testbeans ();// Use a custom class to instantiate Beanlearncreatebean Ctxd = new Learncreatebean ("Bean.xml"); Peopleservice peopleserviced = (peopleservice) ctx.getbean ("Peopleservice");p Eopleservice.testbeans ();}
You can see that both achieve the correct output.
Spring (2)