Simulate Spring framework and spring framework

Source: Internet
Author: User

Simulate Spring framework and spring framework

BeanFactory

1 package com.bjsxt.spring;2 3 public interface BeanFactory {4     public Object getBean(String name);5 }

ClassPathXmlApplicationContext

1 package com. bjsxt. spring; 2 3 import java. lang. reflect. method; 4 import java. util. hashMap; 5 import java. util. list; 6 import java. util. map; 7 8 import org. jdom. document; 9 import org. jdom. element; 10 import org. jdom. input. SAXBuilder; 11 12 public class ClassPathXmlApplicationContext implements BeanFactory {13 private Map <String, Object> beans = new HashMap <String, Object> (); 14 15 public ClassPathXmlApplicationContext () throws Exception {16 SAXBuilder sb = new SAXBuilder (); 17 Document doc = sb. build (this. getClass (). getClassLoader (). getResourceAsStream ("beans. xml "); 18 Element root = doc. getRootElement (); // get the root element 19 List list = root. getChildren ("bean"); // obtain all elements named bean 20 for (int I = 0; I <list. size (); I ++) {21 Element element = (Element) list. get (I); 22 String id = element. getAttributeValue ("id"); 23 String clazz = element. getAttributeValue ("class"); 24 System. out. println (id + ":" + clazz); 25 Object o = Class. forName (clazz ). newInstance (); 26 beans. put (id, o); 27 28 for (Element propertyElement: (List <Element>) element. getChildren ("property") {29 String name = propertyElement. getAttributeValue ("name"); // userDAO30 String bean = propertyElement. getAttributeValue ("bean"); // u31 Object beanObject = beans. get (bean); // UserDAOImpl instance32 33 String methodName = "set" + name. substring (0, 1 ). toUpperCase () + name. substring (1); 34 System. out. println ("method name =" + methodName); 35 36 Method m = o. getClass (). getMethod (methodName, beanObject. getClass (). getInterfaces () [0]); 37 m. invoke (o, beanObject); 38} 39} 40} 41 42 public Object getBean (String name) {43 return beans. get (name); 44} 45 46}

Beans. xml

1 <beans>2     <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean>3     <bean id="userService" class="com.bjsxt.service.UserService">4         <property name="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 package com.bjsxt.dao.impl; 2  3 import com.bjsxt.dao.UserDAO; 4 import com.bjsxt.model.User; 5  6 public class UserDAOImpl implements UserDAO{ 7  8     public void save(User u) { 9         System.out.println("a user saved!");10     }11 12 }

Test class:

 1 package com.bjsxt.service; 2  3 import org.junit.Test; 4  5 import com.bjsxt.model.User; 6 import com.bjsxt.spring.BeanFactory; 7 import com.bjsxt.spring.ClassPathXmlApplicationContext; 8  9 public class UserServiceTest {10 11     @Test12     public void testAddUser() throws Exception {13         BeanFactory factory = new ClassPathXmlApplicationContext();14         15         UserService service = (UserService)factory.getBean("userService");16         User u = new User();17         service.addUser(u);18     }19 20 }

Code link: http://pan.baidu.com/s/1eSoYu8i password: gtj2

Jar package:

Junit link: http://pan.baidu.com/s/1kVFS8lx password: mkh3

Jdom link: http://pan.baidu.com/s/1c1X15Xu password: 9vpi

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.