Spring First day

Source: Internet
Author: User

Spring Framework

1.1: Understanding Spring
Spring's core is to provide a container, mainly through Beanfactory (interface) to create and manage objects, generally we use its subclasses ApplicationContext
To create and manage objects. (The core technology is IOC (control inversion))

The 1.spring IOC container can help us to automate the new object, and we don't have to manually go to the new object after the object is given to the spring tube.
So what is the principle of it? How did it come true? Let me briefly simulate the spring mechanism, I believe that after reading the spring will have a certain understanding of the principle.

2.spring uses Beanfactory to instantiate, configure, and manage objects, but it is just an interface with a Getbean () method.
We generally do not use beanfactory directly, but with its implementation class ApplicationContext, this class will automatically parse our configured Applicationcontext.xml,
Then according to our configured bean to new object, put new object into a map, the key is the ID of our bean, the value is the object of new.

Specific operation:
Step1. First we build a beanfactory interface

 Package com.spring;         Public Interface beanfactory {        Object Getbean (String ID);      }

Step2. Then build a Beanfactory implementation class Classpathxmlapplicationcontext.java

 Packagecom.spring; ImportJava.util.HashMap; Importjava.util.List; ImportJava.util.Map; Importorg.dom4j.Document; Importorg.dom4j.DocumentException; Importorg.dom4j.Element; ImportOrg.dom4j.io.SAXReader;  Public classClasspathxmlapplicationcontextImplementsBeanfactory {Privatemap<string, object> beans =NewHashmap<string, object>();  PublicClasspathxmlapplicationcontext (String fileName)throwsexception{Saxreader Reader=NewSaxreader (); Document Document= Reader.read ( This. GetClass (). getClassLoader (). getResourceAsStream (FileName); List<Element> elements = document.selectnodes ("/beans/bean");  for(Element e:elements) {String ID= E.attributevalue ("id"); String value= E.attributevalue ("Class"); Object o=Class.forName (value). newinstance ();            Beans.put (ID, O); }        }                 PublicObject Getbean (String id) {returnbeans.get (ID); }     }

Step3. Then configure the applicationcontext.xml---name

<?XML version= "1.0" encoding= "UTF-8"?>    <Beans>      <BeanID= "C"class= "Com.spring.Car"></Bean>      <BeanID= "P"class= "Com.spring.Plane"></Bean>    </Beans>

Step4. Creating an interface, by the way, demonstrates Factory mode

 Packagecom.spring;  Public InterfaceMoveable {voidrun (); }     --Automotive class, realize interface Packagecom.spring;  Public classCarImplementsmoveable{ Public voidrun () {System.out.println ("Dragging four wheels across the street car"); }    }    ---aircraft class, realize interface Packagecom.spring;  Public classPlaneImplementsmoveable{ Public voidrun () {System.out.println ("Drag the wings of the sky to fly plane ..."); }        }

Step5. Now look at the effect, write a class test:

 Packagecom.spring; Importorg.dom4j.DocumentException;  Public classTest {/**     * @paramargs *@throwsdocumentexception*/     Public Static voidMain (string[] args)throwsException {beanfactory Factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Object o= Factory.getbean ("C"); Moveable M=(moveable) o;    M.run (); }    }

Spring First day

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.