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
- 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 {
- //The core of the container, used to store injected beans
- private map<string, object> container = new hashmap<string, object> ();
- //Parse XML file, put configured bean into container by 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 the bean in the configuration file
- For (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);
- }
- }
- @Override
- Public Object Getbean (String id) {
- return Container.get (ID);
- }
- }
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
- <? 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 above configuration file by invoking the previous Classpathxmlapplicationcontext, and then it can get the bean we need by ID:
[Java]View PlainCopy
- Package com.tgb.spring.factory;
- Public class Test {
- public static void Main (string[] args) throws Exception {
- //Load configuration file
- Beanfactory f = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
- //England
- Object oe = F.getbean ("E");
- Team E = (team) OE;
- E.say ();
- //Spain
- Object OS = F.getbean ("S");
- Team S = (team) OS;
- S.say ();
- //Portugal
- Object op = F.getbean ("P");
- Team P = (team) op;
- P.say ();
- }
- }
Output Result:
[Plain]View PlainCopy
- England: We are the Chinese team in Europe, do not care about this group is not qualified ...
- Spain: We are the two European Cup champions and one World Cup winner!
- Portugal: Our Cristiano Ronaldo is a top 10!
Other code:
[Java]View PlainCopy
- 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 ("England: We are the Chinese team in Europe, do not care if this group is not qualified ...");
- }
- }
- Spain
- Package com.tgb.spring.factory;
- Public class Spain implements team{
- @Override
- public Void Say () {
- System.out.println ("Spain: We are the two European Cup champions and one World Cup winner!") ");
- }
- }
- Portugal
- Package com.tgb.spring.factory;
- Public class Portugal implements Team {
- @Override
- public Void Say () {
- System.out.println ("Portugal: Our Cristiano Ronaldo a top 10! ");
- }
- }
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