Spring manages bean containers (notes) [following]

Source: Internet
Author: User

After learning about the instantiation of spring containers, we can simply simulate this process: 1. first load the xml configuration file of spring 2. parse the configuration file and map the node into a Class 3. instantiate the <bean and exist in a set map. 4. then bean according to the id in this collection. Copy the code mybens. xml configuration file <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <! -- Use the class constructor to instantiate --> <bean id = "refrectShili" class = "cn. can. dao. impl. refrectShili "> </bean> </beans> copy the class corresponding to the Code node. Here, only the id and class types are stored, the xml configuration is used to instantiate and copy the code package cn using the bean class constructor. can. myspring; // node in xml // <bean id = "refrectShili" class = "cn. can. dao. impl. public class MyBean {private String id; private String className; public MyBean () {super ();} public MyBean (String id, string className ){ Super (); this. id = id; this. className = className;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getClassName () {return className;} public void setClassName (String className) {this. className = className;} The Key to copying the code is the following parsing xml and instantiation. The xpath parsing xml and reflection mechanisms are used to instantiate the class. Copy the code package cn. can. myspring; 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; import cn. can. myspring. exception. noIdClassException; public class MyApplicationContext {// because beans are multiple private lists <MyBean> myBeans = new ArrayList <MyB Ean> (); // store the reference private Map after instantiation <String, Object> singles = new HashMap <String, Object> (); // an xml can be input here, alternatively, you can input multiple xml public MyApplicationContext () {} public Object getBean (String id) throws NoIdClassException {if (singles. size () <= 0) {throw new NoIdClassException ("this class does not exist");} Object obj = singles. get (id); if (obj = null) {throw new NoIdClassException ("this class does not exist");} return obj;} public MyApplicationC Ontext (String fileName) {init (fileName); // After obtaining xml Information, initObject (); // instantiate <bean} // 2. instantiate private void initObject () {try {if (myBeans! = Null & myBeans. size ()> 0) {// traverse bean // <bean node in xml, which can be abstracted into a class for (MyBean bean: myBeans) {String id = bean. getId (); String clazz = bean. getClassName (); if (id! = Null &&! "". Equals (id) {if (clazz! = Null &&! "". Equals (clazz) {// instantiate Class cl = Class at this time. forName (clazz); Object o = cl. newInstance (); singles. put (id, o) ;}}}} catch (Exception e) {}} public MyApplicationContext (String [] fileNames) {}// 1. load the xml file and parse the xml file private void init (String fileName) {SAXReader saxReader = new SAXReader (); Document document = null; try {URL xmlpath = this. getClass (). getClassLoader (). getResource (fileName); document = saxReader. read (xmlpath); Map <String, String> nsMap = new HashMap <String, String> (); nsMap. put ("ns", "http://www.springframework.org/schema/beans"); // Add the namespace XPath xsub = document. createXPath ("// ns: beans/ns: bean"); // create the beans/bean query path xsub. setNamespaceURIs (nsMap); // set the namespace List <Element> beans = xsub. selectNodes (document); // obtain all bean nodes under the document for (Element e: beans) {String id = e. attributeValue ("id"); // obtain the id attribute value String clazz = e. attributeValue ("class"); // obtain the class attribute value MyBean myBean = new MyBean (id, clazz); // obtain a <bean> </bean> myBeans. add (myBean); // parse all beans in xml} catch (Exception e ){}}}

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.