Write your own spring frame (1)-ApplicationContext container

Source: Internet
Author: User

Directly on the source:

Beans.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd ">6     <BeanID= "Personservicebean"class= "Service.imp.PersonServiceBean"></Bean>7 </Beans>

Personservice.java

 Package Service;  Public Interface Personservice {    publicvoid  Sava ();}

Personservicebean.java

1  PackageService.imp;2 3 Importservice. Personservice;4 5 6  Public classPersonservicebeanImplementsPersonservice {7     8      Public voidSava () {9System.out.println ("Execute Save Method");Ten     } One}

Beandefinition.java

1  Packagejunit.test;2 3  Public classbeandefinition {4     PrivateString Idvalue;//Save ID Property5     PrivateString Classvalue;//Save Class Property6      PublicString Getidvalue () {7         returnIdvalue;8     }9      Public voidSetidvalue (String idvalue) {Ten          This. Idvalue =Idvalue; One     } A      PublicString Getclassvalue () { -         returnClassvalue; -     } the      Public voidSetclassvalue (String classvalue) { -          This. Classvalue =Classvalue; -     } -      Publicbeandefinition (String idvalue, String classvalue) { +         Super(); -          This. Idvalue =Idvalue; +          This. Classvalue =Classvalue; A     } at      -      -}

Xiaowenapplicationcontext.java

1  Packagejunit.test;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importorg.dom4j.Document;9 Importorg.dom4j.Element;Ten ImportOrg.dom4j.io.SAXReader; One /*** A * Mission Objective: To emulate the spring container and write a small version of the spring container - * Step: 1. Read the XML file - * 2. Read <beans> all the <bean> ID and Class attribute values, save the attribute in the object, and then put the object in the list.  the * 3. Iterate through the list, read the ID and class attribute values stored in the object, and use the reflection mechanism to instantiate the class object. Save the id attribute and the class instance object's key value pair in the map.  - * 4. Remove the object from the map by Getbean ().  -  */ -  Public classXiaowenapplicationcontext { +     PrivateList<beandefinition> Beandefines =NewArraylist<beandefinition> ();//Read the ID and class attributes and save the attribute values in the Beandefinition object.  -     PrivateMap<string,object> sigletons =NewHashmap<string, object> ();//instantiate the bean, save the bean in the map +      PublicXiaowenapplicationcontext (String fileName) { A          This. ReadXML (fileName); at          This. Instancebeans (); -     } -     /**instantiating a Bean object*/ -     Private voidInstancebeans () { -          for(beandefinition beandefinition:beandefines) { -             Try { inString idvalue = Beandefinition.getidvalue ();//Read ID Property -String Classvalue = Beandefinition.getclassvalue ();//Read Class Property to                 if(classvalue!=NULL&&! "". Equals (Classvalue.trim ())) { + sigletons.put (Idvalue, Class.forName (classvalue). newinstance ()); -                 } the}Catch(Exception e) { * e.printstacktrace (); $             }Panax Notoginseng         } -     } the     /**get the Bean object from the map*/ +      PublicObject Getbean (String beanname) { A         returnSigletons.get (beanname); the     } +      -     /**using dom4j to read XML files*/ $     Private voidReadXML (String fileName) { $Saxreader reader =NewSaxreader (); -         Try { -Document doc= reader.read ("src/" +filename);//Reader () is read by default from the project root path, and spring XML is placed by default in the SRC folder theElement root = Doc.getrootelement ();//Get root node -list<element> elements = root.elements ("Bean");Wuyi              for(Element element:elements) {//Cyclic Iteration Elements theString idvalue = element.attributevalue ("id");//Get ID Property -String Classvalue = Element.attributevalue ("class");//get the Class property WuBeandefinition beandefinition =NewBeandefinition (Idvalue, Classvalue);//save ID and class attributes - Beandefines.add (beandefinition); About             } $}Catch(Exception e) { - e.printstacktrace (); -         } -     } A}

Xiaowenspringtest.java

1  Packagejunit.test;2 3 Importorg.junit.Test;4 5 Importservice. Personservice;6 7  Public classXiaowenspringtest {8 @Test9      Public voidinstancespring () {TenXiaowenapplicationcontext AC =NewXiaowenapplicationcontext ("Beans.xml"); OnePersonservice service = (personservice) ac.getbean ("Personservicebean"); A Service.sava (); -     } -}

Write your own spring frame (1)-ApplicationContext container

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.