Spring (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.