Simulate Spring functionality

Source: Internet
Author: User
1, the establishment of User.java, the contents are as follows:

Package net.model;/** * @ Project Name: spring2.5 * @ Pack Name: Net.model * @ file name: User.java * @ Date: June, 4:31:22 PM * @ remark: * @ Author: Apple * /public class User {private string Username;private string Password;public string GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

2, the establishment of Userdao.java, the contents are as follows:

Package Net.dao;import net.model.user;/** * @ project Name: spring2.5 * @ Pack Name: Net.dao * @ file name: Userdao.java * @ Date: June, 4:12:42 PM * @ Note: * @ Author: Apple */public interface Userdao {public void Save (User u);}

3, the establishment of Userdaoimpl.java, the contents are as follows:

Package Net.dao.impl;import net.dao.userdao;import net.model.user;/** * @ project Name: spring2.5 * @ Pack Name: Net.dao.impl * @ File name: Userdaoimpl.java * @ Date: June, 4:13:45 PM * @ Note: * @ Author: Apple */public class Userdaoimpl implements Userdao {Publi c void Save (User u) {//TODO auto-generated method StubSystem.out.println ("User save ...");}}

4, the establishment of Userservice.java, the contents are as follows:

Package Net.service;import Net.dao.userdao;import net.dao.impl.userdaoimpl;import net.model.user;/** * @ Project Name: spring2.5 * @ Package Name: Net.service * @ file name: Userservice.java * @ Date: June, 4:15:47 PM * @ remark: * @ Author: Apple */public class Us Erservice {private Userdao Userdao;p ublic Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} public void Add (User u) {userdao.save (U);} You can invoke the task here to implement the Save method for the Userdao interface. }

5, the establishment of Beanfactory.java, the contents are as follows:

Package net.factory;/** * @ Project Name: spring2.5 * @ Pack Name: Net.factory * @ file name: Beanfactory.java * @ Date: June, 4:37:08 PM * @ remark: * @ Author: Apple */public interface beanfactory {public Object Getbean (String name);}

6, the establishment of Classpathxmlapplicationcontext.java, the contents are as follows:

Package Net.util;import Java.lang.reflect.method;import Java.util.hashmap;import java.util.list;import Java.util.map;import Net.dao.userdao;import Net.factory.beanfactory;import Net.model.user;import Net.service.userservice;import org.jdom.document;import org.jdom.element;import org.jdom.input.SAXBuilder;/** * @ Project Name: spring2.5 * @ Package Name: Net.util * @ file name: Classpathxmlapplicationcontext.java * @ Date: June, 4:19:35 PM * @ remark: * @ Author: Apple * /public class Classpathxmlapplicationcontext implements beanfactory{private map<string,object> beans = new    Hashmap<string, object> ();  Public Classpathxmlapplicationcontext () throws exception{Saxbuilder sb=new saxbuilder ();//Create a Saxbuilder object Document Doc=sb.build (ClassPathXmlApplicationContext.class.getClassLoader (). getResourceAsStream ("Beans.xml")); Constructs a Document Object Element root=doc.getrootelement (); Gets the root element List List=root.getchildren ("bean");//Name All elements of the bean for (int i=0;i<list.size (); i++) {element ele Ment= (Element) list.geT (i);          String id = element.getattributevalue ("id");          String clazz = Element.getattributevalue ("class");          System.out.println (id + ":" + clazz);          Object o = Class.forName (clazz). newinstance ();           Beans.put (ID, O); * The following for loop is implemented to simulate spring automatic assembly (injection) function//Start listing all property child elements of this bean for (element propertyelement: (list<element>) Element.getchildren ("property")) {//Gets the value (i.e. the name of the parameter that needs to be injected) in the properties child element of the attribute in String name = Propertyelement.getattr      Ibutevalue ("name");      Gets the value of the property child element in the bean (the type that needs to inject the parameter), where the bean value is the ID of the bean that has been initialized well above.      String bean = propertyelement.getattributevalue ("Bean");            So the bean with the specified ID is obtained here Object Beanobject = Beans.get (bean);      Make a set method name: Set + First Letter Capital + other letter String MethodName = "Set" + name.substring (0,1). toUpperCase () + name.substring (1);      System.out.println ("methodName =" + MethodName); Gets the set method of the bean, the parameter (method name, parameter: The type of this parameter) method M = O.getclass (). GetMethod (MethodnamE, Beanobject.getclass (). getinterfaces () [0]);      Using the reflection mechanism, the method is implemented to implement the injection function M.invoke (o, beanobject);        }}} public Object Getbean (String name) {return beans.get (name);}  public static void Main (string[] args) {beanfactory Factory=null;try {factory = new Classpathxmlapplicationcontext ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} UserService service = (userservice) factory.getbean ("UserService");    User U = new user (); service.add (U); }}

7, the establishment of beans.xml, the contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans><bean id= "U" class= "Net.dao.impl.UserDaoImpl" ></ Bean><bean id= "UserService" class= "Net.service.UserService" ><property name= "Userdao" bean= "U"/>< /bean></beans>
  • Related Article

    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.