模拟spring

Posted popcorn丫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟spring相关的知识,希望对你有一定的参考价值。

实体类:

技术分享
 1 package com.bxw.po;
 2 
 3 public class User {
 4     private String username;    //账号
 5     private String password;    //密码
 6     public String getUsername() {
 7         return username;
 8     }
 9     public void setUsername(String username) {
10         this.username = username;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18     
19     
20 }
user

DAO:

技术分享
1 package com.bxw.dao;
2 
3 import com.bxw.po.User;
4 
5 public interface UserDao {
6     public void save(User u);    
7 }
UserDao.java

Dao实现:

技术分享
 1 package com.bxw.dao.impl;
 2 
 3 import com.bxw.dao.UserDao;
 4 import com.bxw.po.User;
 5 
 6 public class UserDaoImpl implements UserDao {
 7     public void save(User u){
 8         System.out.println("user saved");
 9     }
10 }
UserDaoImpl

Service:

技术分享
 1 package com.bxw.service;
 2 
 3 import com.bxw.dao.UserDao;
 4 import com.bxw.dao.impl.UserDaoImpl;
 5 import com.bxw.po.User;
 6 
 7 public class UserService {
 8     private UserDao userDao = new UserDaoImpl();
 9 
10     public UserDao getUserDao() {
11         return userDao;
12     }
13 
14     public void setUserDao(UserDao userDao) {
15         this.userDao = userDao;
16     }
17 
18     
19     public void add(User u){
20         this.userDao.save(u);
21     }
22     
23 }
UserService

配置文件:

技术分享
1 <beans>
2     <bean id="userDao" class="com.bxw.dao.impl.UserDaoImpl" />
3     <bean id="userService" class="com.bxw.service.UserService">
4         <property name="userDao" bean="userDao" />
5     </bean>
6 </beans>
beans.xml

BeanFactory:

技术分享
1 package com.bxw.spring;
2 
3 public interface BeanFactory {
4     public Object getBean(String name);
5 }
BeanFactory.java

ClassPathXMLApplicationContext:(需要jdom.jar)

技术分享
 1 package com.bxw.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();    //获取根元素
19         List<Element> list = root.getChildren("bean");
20         for(int i=0;i<list.size();i++){
21             Element element = (Element)list.get(i);
22             String id = element.getAttributeValue("id");    //属性id
23             String cls = element.getAttributeValue("class");//class
24             System.out.println(id+" "+cls);
25             Object obj = Class.forName(cls).newInstance();//反射拿到类
26             beans.put(id, obj);
27             for(Element propertyElement:(List<Element>)element.getChildren("property")){    
28                 String name = propertyElement.getAttributeValue("name");    //userDao
29                 String bean = propertyElement.getAttributeValue("bean");    //userDao
30                 Object beanObject = beans.get(bean);
31                 String methodName = "set"+name.substring(0,1).toUpperCase()+name.substring(1);    //得到setUserDao方法
32                 System.out.println(methodName);
33                 
34                 Method m = obj.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
35                 m.invoke(obj, beanObject);
36             }
37         }
38     }
39     @Override
40     public Object getBean(String name) {
41         return beans.get(name);
42     }
43     
44 }
ClassPathXMLApplicationContext.java

测试类:这里使用junit测试

技术分享
 1 package com.bxw.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.jboss.weld.context.ApplicationContext;
 6 import org.junit.Test;
 7 
 8 import com.bxw.dao.UserDao;
 9 import com.bxw.po.User;
10 import com.bxw.service.UserService;
11 import com.bxw.spring.BeanFactory;
12 import com.bxw.spring.ClassPathXMLApplicationContext;
13 
14 public class UserServiceTest {
15 
16     @Test
17     public void testAdd() throws Exception{
18         BeanFactory ac = new ClassPathXMLApplicationContext();    //初始化所有bean
19         UserService service = (UserService) ac.getBean("userService");    //拿到id为userService,依赖注入userDao
20         User u = new User();
21         service.add(u);
22     }
23 }
UserServiceTest.java

 

以上是关于模拟spring的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

iOS Swift 中的 Android 片段模拟