Spring IOC container parsing and implementation principles

Source: Internet
Author: User
Tags xpath

In recent times, "container" two words have been lingering in my ears, even eat, sleep in my mind jumping to jump. As these days of exchanges and discussions, the understanding of the container gradually deepened. In theory, things have to be implemented to practice, today, with the help of spring container implementation principle, simply say it.

To put it simply, spring is putting our beans in its container through Factory + reflection, and when we want to use a bean, we just need to call the Getbean ("Beanid") method.

The principle of simple introduction:

The principle of the spring container is actually to parse the XML file, or fetch the user-configured bean, and then put the beans into the collection by reflection, then provide a Getbean () method for us to get the beans. The following is a simple analog code:

[Java]View PlainCopy 
  1. Package com.tgb.spring.factory;
  2. Import Java.util.HashMap;
  3. Import java.util.List;
  4. Import Java.util.Map;
  5. Import org.jdom.Document;
  6. Import org.jdom.Element;
  7. Import Org.jdom.input.SAXBuilder;
  8. Import Org.jdom.xpath.XPath;
  9. Public class Classpathxmlapplicationcontext implements Beanfactory {
  10. //The core of the container, used to store injected beans
  11. private map<string, object> container = new hashmap<string, object> ();
  12. //Parse XML file, put configured bean into container by reflection
  13. Public Classpathxmlapplicationcontext (String fileName) throws exception{
  14. Saxbuilder sb = new Saxbuilder ();
  15. Document doc = Sb.build (this.getclass (). getClassLoader (). getResourceAsStream (FileName));
  16. Element root = Doc.getrootelement ();
  17. List List = Xpath.selectnodes (root, "/beans/bean");
  18. //Scan the bean in the configuration file
  19. For (int i = 0; i < list.size (); i++) {
  20. Element bean = (Element) list.get (i);
  21. String id = bean.getattributevalue ("id");
  22. String clazz = Bean.getattributevalue ("class");
  23. Object o = Class.forName (clazz). newinstance ();
  24. Container.put (ID, O);
  25. }
  26. }
  27. @Override
  28. Public Object Getbean (String id) {
  29. return Container.get (ID);
  30. }
  31. }

First declare a map of the bean, and then through the Jdom parse the configuration file, loop through all the <bean> nodes and put them through reflection into the map we declared earlier. Then provide a method of Getbean (), so that we can find the bean we want through the bean ID.

The following is a simple XML configuration file:

[HTML]View PlainCopy 
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans>
  3. <Bean id="E" class="Com.tgb.spring.factory.England" />
  4. <Bean id="S" class="Com.tgb.spring.factory.Spain" />
  5. <Bean id="P" class="Com.tgb.spring.factory.Portugal" />
  6. </Beans>



The client loads the above configuration file by invoking the previous Classpathxmlapplicationcontext, and then it can get the bean we need by ID:

[Java]View PlainCopy 
  1. Package com.tgb.spring.factory;
  2. Public class Test {
  3. public static void Main (string[] args) throws Exception {
  4. //Load configuration file
  5. Beanfactory f = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
  6. //England
  7. Object oe = F.getbean ("E");
  8. Team E = (team) OE;
  9. E.say ();
  10. //Spain
  11. Object OS = F.getbean ("S");
  12. Team S = (team) OS;
  13. S.say ();
  14. //Portugal
  15. Object op = F.getbean ("P");
  16. Team P = (team) op;
  17. P.say ();
  18. }
  19. }


Output Result:

[Plain]View PlainCopy  
    1. England: We are the Chinese team in Europe, do not care about this group is not qualified ...
    2. Spain: We are the two European Cup champions and one World Cup winner!
    3. Portugal: Our Cristiano Ronaldo is a top 10!


Other code:

[Java]View PlainCopy 
  1. Factory interface
  2. Package com.tgb.spring.factory;
  3. Public interface Beanfactory {
  4. Object Getbean (String ID);
  5. }
  6. Team Interface
  7. Package com.tgb.spring.factory;
  8. Public interface Team {
  9. void Say ();
  10. }
  11. England
  12. Package com.tgb.spring.factory;
  13. Public class England implements team{
  14. public Void Say () {
  15. System.out.println ("England: We are the Chinese team in Europe, do not care if this group is not qualified ...");
  16. }
  17. }
  18. Spain
  19. Package com.tgb.spring.factory;
  20. Public class Spain implements team{
  21. @Override
  22. public Void Say () {
  23. System.out.println ("Spain: We are the two European Cup champions and one World Cup winner!")  ");
  24. }
  25. }
  26. Portugal
  27. Package com.tgb.spring.factory;
  28. Public class Portugal implements Team {
  29. @Override
  30. public Void Say () {
  31. System.out.println ("Portugal: Our Cristiano Ronaldo a top 10!  ");
  32. }
  33. }


The above is a simple simulation of spring, of course, spring is much more complex and powerful than this, and the way to get beans is not only through the factory. This is just a rough demo of a simple understanding of the container and a tribute to spring. Examples humble, express rough, welcome to shoot Brick Exchange.

Spring IOC container parsing and implementation principles

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.