模拟Spring的 IOC 和 DI

[size=xx-large][color=orange][b]模拟Spring的 IOC 和 DI[/b][/color][/size]
[size=medium][color=red][b]1.BeanFactory 接口[/b][/color][/size]

public interface BeanFactory {
public Object getBean(String id);
}


[size=medium][color=red][b]2.ClassPathXmlApplicationContex实现类[/b][/color][/size]

public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String , Object> beans = new HashMap<String, Object>();

/**
* 模拟控制反转(IOC)+依赖注入(DI)
* IOC Inverse of Control
* DI Dependency Injection
*/
public ClassPathXmlApplicationContext() throws Exception {
SAXBuilder sb=new SAXBuilder();

Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象
Element root=doc.getRootElement(); //获取根元素HD
List list=root.getChildren("bean");//取名字为disk的所有元素
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
String id=element.getAttributeValue("id");
String clazz=element.getAttributeValue("class");
Object o = Class.forName(clazz).newInstance();
System.out.println(id);
System.out.println(clazz);
beans.put(id, o);

for(Element propertyElement : (List<Element>)element.getChildren("property")) {
String name = propertyElement.getAttributeValue("name"); //userDAO
String bean = propertyElement.getAttributeValue("bean"); //u
Object beanObject = beans.get(bean);//UserDAOImpl instance

String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.println("method name = " + methodName);

Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
m.invoke(o, beanObject);
}
}
}

public Object getBean(String id) {
return beans.get(id);
}
}


[size=medium][color=red][b]3.applicationContext-beans.xml配置文件[/b][/color][/size]

<beans>
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" />
<bean id="userService" class="com.bjsxt.service.UserService" >
<property name="userDAO" bean="u"/>
</bean>
</beans>


[size=medium][color=red][b]4.UserDAO接口和UserDAOImpl实现类[/b][/color][/size]

public interface UserDAO {
public void save(User user);
}

public class UserDAOImpl implements UserDAO {
public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
}
}


[size=medium][color=red][b]5.UserService接口和实现类UserServiceImpl[/b][/color][/size]

public interface UserService {
public void add(User user);
}

public class UserServiceImpl implements UserService{
private UserDAO userDAO;
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}


[size=medium][color=red][b]6.测试类[/b][/color][/size]

import com.momo.spring.BeanFactory;
import com.momo.spring.ClassPathXmlApplicationContext;

public class IOCTest {
@Test
public void testAdd() throws Exception {
BeanFactory applicationContext = new ClassPathXmlApplicationContext();
UserService service = (UserService)applicationContext.getBean("userService");
User u = new User();
u.setUsername("zhangsan");
u.setPassword("zhangsan");
service.add(u);
}
}


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest {
@Test
public void testAdd() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//BeanFactory也可以
UserService service = (UserService)ctx.getBean("userService");
User u = new User();
u.setUsername("zhangsan");
u.setPassword("zhangsan");
service.add(u);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值