Simulating Spring frames

Source: Internet
Author: User

Beanfactory

1  Package com.bjsxt.spring; 2 3  Public Interface beanfactory {4      Public Object Getbean (String name); 5 }

Classpathxmlapplicationcontext

1  Packagecom.bjsxt.spring;2 3 ImportJava.lang.reflect.Method;4 ImportJava.util.HashMap;5 Importjava.util.List;6 ImportJava.util.Map;7 8 Importorg.jdom.Document;9 Importorg.jdom.Element;Ten ImportOrg.jdom.input.SAXBuilder; One  A  Public classClasspathxmlapplicationcontextImplementsbeanfactory{ -     Privatemap<string, object> beans =NewHashmap<string, object>(); -      the      PublicClasspathxmlapplicationcontext ()throwsexception{ -Saxbuilder SB =NewSaxbuilder (); -Document doc = Sb.build ( This. GetClass (). getClassLoader (). getResourceAsStream ("Beans.xml")); -Element root = Doc.getrootelement ();//Get root element +List List = Root.getchildren ("Bean");//name all the elements of the bean -          for(inti=0; I<list.size (); i++){ +Element element =(Element) list.get (i); AString id = element.getattributevalue ("id"); atString clazz = Element.getattributevalue ("Class"); -System.out.println (id + ":" +clazz); -Object o =Class.forName (clazz). newinstance (); - beans.put (ID, o); -              -              for(Element propertyelement: (list<element>) Element.getchildren ("property"))){ inString name = Propertyelement.getattributevalue ("name");//Userdao -String bean = propertyelement.getattributevalue ("Bean");//u toObject beanobject = Beans.get (bean);//Userdaoimpl Instance +                  -String methodName = "Set" + name.substring (0, 1). toUpperCase () + name.substring (1); theSystem.out.println ("Method name =" +methodName); *                  $Method m = O.getclass (). GetMethod (MethodName, Beanobject.getclass (). getinterfaces () [0]);Panax Notoginseng M.invoke (o, beanobject);  -             } the         } +     } A  the      PublicObject Getbean (String name) { +         returnbeans.get (name); -     } $      $}

Beans.xml

1 <Beans>2     <BeanID= "U"class= "Com.bjsxt.dao.impl.UserDAOImpl"></Bean>3     <BeanID= "UserService"class= "Com.bjsxt.service.UserService">4         < Propertyname= "Userdao"Bean= "U"></ Property>5     </Bean>6 </Beans>

Userdao

1  Package Com.bjsxt.dao; 2 3 Import Com.bjsxt.model.User; 4 5  Public Interface Userdao {6      Public void Save (User u); 7 }

Userdaoimpl

1  PackageCom.bjsxt.dao.impl;2 3 ImportCom.bjsxt.dao.UserDAO;4 ImportCom.bjsxt.model.User;5 6  Public classUserdaoimplImplementsuserdao{7 8      Public voidSave (User u) {9System.out.println ("A user saved!");Ten     } One  A}

Test class:

1  PackageCom.bjsxt.service;2 3 Importorg.junit.Test;4 5 ImportCom.bjsxt.model.User;6 Importcom.bjsxt.spring.BeanFactory;7 ImportCom.bjsxt.spring.ClassPathXmlApplicationContext;8 9  Public classUserservicetest {Ten  One @Test A      Public voidTestadduser ()throwsException { -Beanfactory factory =NewClasspathxmlapplicationcontext (); -          theUserService service = (userservice) factory.getbean ("UserService"); -User U =NewUser (); - service.adduser (u); -     } +  -}

Code Link: http://pan.baidu.com/s/1eSoYu8i Password: gtj2

Required JAR Packages:

JUnit Link: http://pan.baidu.com/s/1kVFS8lx Password: mkh3

Jdom Link: http://pan.baidu.com/s/1c1X15Xu Password: 9VPI

Simulating Spring frames

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.