Cainiao SSH (13)-Spring container parsing and simple implementation

Source: Internet
Author: User

Recently, the word "Container" has been lingering in my ears, and even when I eat or go to bed, it is in my mind. With the communication and discussion these days, the understanding of containers is gradually deepened. In theory, things will eventually be put into practice. Today we will use Spring containers to implement the principle. Let's just put it simply.

In short, Spring puts our bean into its container through factory + reflection. When we want to use a bean, we only need to call the getBean ("beanID") method.


Brief Introduction to the principle:

The principle of the Spring container is to parse the xml file or retrieve the bean configured by the user, and place these beans in the collection by reflection. Then, a getBean () method is provided for external users, so that we can get these beans. The following is a simple simulated code:

Package com. tgb. spring. factory; import java. util. hashMap; import java. util. list; import java. util. map; import org. jdom. document; import org. jdom. element; import org. jdom. input. SAXBuilder; import org. jdom. xpath. XPath; public class ClassPathXmlApplicationContext implements BeanFactory {// container core, used to store injected Beanprivate Map <String, Object> container = new HashMap <String, Object> (); // parse the xml file and put the configured bean into the container through reflection. public ClassPathXmlApplicationContext (String fileName) throws Exception {SAXBuilder sb = new SAXBuilder (); Document doc = sb. build (this. getClass (). getClassLoader (). getResourceAsStream (fileName); Element root = doc. getRootElement (); List list = XPath. selectNodes (root, "/beans/bean"); // scan beanfor (int I = 0; I <list. size (); I ++) {Element bean = (Element) list. get (I); String id = bean. getAttributeValue ("id"); String clazz = bean. getAttributeValue ("class"); Object o = Class. forName (clazz ). newInstance (); container. put (id, o) ;}@ Overridepublic Object getBean (String id) {return container. get (id );}}

Declare a Map that stores beans, parse the configuration file through jdom, traverse all <bean> nodes cyclically, and put them in the previously declared Map through reflection. Then we provide a getBean () method, so that we can find the bean we want through the bean Id.


The following is a simple xml configuration file:

<?xml version="1.0" encoding="UTF-8"?><beans>  <bean id="E" class="com.tgb.spring.factory.England" />    <bean id="S" class="com.tgb.spring.factory.Spain" />    <bean id="P" class="com.tgb.spring.factory.Portugal" /></beans>


The client loads the preceding configuration file by calling the ClassPathXmlApplicationContext, and then obtains the bean we need through the Id:

Package com. tgb. spring. factory; public class Test {public static void main (String [] args) throws Exception {// load the configuration file BeanFactory f = new ClassPathXmlApplicationContext ("applicationContext. xml "); // England Object oe = f. getBean ("E"); Team e = (Team) oe; e. say (); // Spanish Object OS = f. getBean ("S"); Team s = (Team) OS; s. say (); // Portuguese Object op = f. getBean ("P"); Team p = (Team) op; p. say ();}}

Output result:

England: We are the Chinese team in Europe. We don't care if this team is not qualified... Spain: We are two European Cup champions and One World Cup champion! Portugal: We have ten slots!

Other code:

// Factory interface package com. tgb. spring. factory; public interface BeanFactory {Object getBean (String id);} // Team interface package com. tgb. spring. factory; public interface Team {void say ();} // England package com. tgb. spring. factory; public class England implements Team {public void say () {System. out. println... ") ;}} // Spanish package com. tgb. spring. factory; public class Spain implements Team {@ Overridepubli C void say () {System. out. println ("Spain: We are two European Cup champions and One World Cup champion! ") ;}} // Portuguese package com. tgb. spring. factory; public class Portugal implements Team {@ Overridepublic void say () {System. out. println ("Portugal: our Cristiano Ronaldo has a top ten! ");}}

The above content is a simple simulation of Spring. Of course, Spring is much more complicated and powerful than this, and the way to obtain beans is not only through the factory. Here is just a rough Demo to show you a simple understanding of the container and pay tribute to Spring. The example is simple and the expression is rough. You are welcome to make a discussion.



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.