一 模拟一个spring(依赖注入)
UserDAO.java============用于访问数据库的抽象,是一个接口
public interface UserDAO {
public void save(User u);
}
UserDAOImpl.java=======这个是它的具体实现(设计成接口有利于实现多种数据的访问1)
public class UserDAOImpl implements UserDAO{
@Override
public void save(User u) {
System.out.println("a user save");
}
}
UserService.java=========这个是用来调用dao的服务
public class UserService {
private UserDAO userDAO;
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
UserServiceTest.java===这个是JUnit test
//使用JUnit 就直接在对应的类,右键选择JUnit test 然后选择要test的方法完成就可以用了
public class UserServiceTest {
public void testAdd() throws Exception {
/*本来我们是这样做的*/
/*
UserService service = new UserService();
UserDAO userDAO = new UserDAOImpl();
service.userDAO = userDAO;
User u = new User();
service.add(u);
*/
/*使用spring后,我们就可以直接用工厂创建对应的实例,很明显这样更有利于各组件的解耦*/
BeanFactory factory = new ClassPathXmlApplicationContext();
UserService service = (UserService)factory.getBean("userService");
User u = new User();
service.add(u);
}
}
BeanFactory.java=================bean工厂,必须实现这个借口
public interface BeanFactory {
public Object getBean(String name);
}
ClassPathXmlApplicationContext.java====通过反射机制来实例bean
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String , Object> beans = new HashMap<String, Object>();
//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);
}
}
bean.xml=============配置文件
<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>
UserDAO.java============用于访问数据库的抽象,是一个接口
public interface UserDAO {
public void save(User u);
}
UserDAOImpl.java=======这个是它的具体实现(设计成接口有利于实现多种数据的访问1)
public class UserDAOImpl implements UserDAO{
@Override
public void save(User u) {
System.out.println("a user save");
}
}
UserService.java=========这个是用来调用dao的服务
public class UserService {
private UserDAO userDAO;
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
UserServiceTest.java===这个是JUnit test
//使用JUnit 就直接在对应的类,右键选择JUnit test 然后选择要test的方法完成就可以用了
public class UserServiceTest {
public void testAdd() throws Exception {
/*本来我们是这样做的*/
/*
UserService service = new UserService();
UserDAO userDAO = new UserDAOImpl();
service.userDAO = userDAO;
User u = new User();
service.add(u);
*/
/*使用spring后,我们就可以直接用工厂创建对应的实例,很明显这样更有利于各组件的解耦*/
BeanFactory factory = new ClassPathXmlApplicationContext();
UserService service = (UserService)factory.getBean("userService");
User u = new User();
service.add(u);
}
}
BeanFactory.java=================bean工厂,必须实现这个借口
public interface BeanFactory {
public Object getBean(String name);
}
ClassPathXmlApplicationContext.java====通过反射机制来实例bean
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String , Object> beans = new HashMap<String, Object>();
//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);
}
}
bean.xml=============配置文件
<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>