package com.dao;
import com.entry.Student;
public interface StudentDao {
void add(Student student);
}
----------
package com.dao;
import com.entry.Student;
public class StudentDaoImpl implements StudentDao {
@Override
public void add(Student student) {
System.out.println(student.getId() + "----" + student.getName());
}
}
----------
package com.entry;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------
package com.service;
import com.entry.Student;
public interface StudentService {
void add(Student student);
}
----------
package com.service;
import com.dao.StudentDao;
import com.entry.Student;
public class StudentServiceImpl implements StudentService {
private StudentDao stuDao;
public StudentDao getStuDao() {
return stuDao;
}
public void setStuDao(StudentDao stuDao) {
this.stuDao = stuDao;
}
@Override
public void add(Student student) {
System.out.println("student was saved!");
stuDao.add(student);
}
}
----------
package com.spring.ioc;
/**
*只是为了模拟spring,本程序中此接口毫无意义
*/
public interface BeanFactory {
Object getBean(String id);
}
----------
package com.spring.ioc;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String, Object> beans = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
public ClassPathXmlApplicationContext() throws JDOMException, IOException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException, SecurityException,
IllegalArgumentException, InvocationTargetException {
// 1.读取配置文件,生成docment对象
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(this.getClass().getClassLoader()
.getResourceAsStream("beans.xml"));
Element root = doc.getRootElement();
List<Element> list = root.getChildren("bean");
// 2.将对象实例化放入容器
for (int i = 0; i < list.size(); i++) {
Element ele = list.get(i);
String id = ele.getAttributeValue("id");
String clazz = ele.getAttributeValue("class");
Object obj = Class.forName(clazz).newInstance();
beans.put(id, obj);
// 3.遍历property注入对象
for (Element property : (List<Element>) ele.getChildren("property")) {
String name = property.getAttributeValue("name");
String bean = property.getAttributeValue("bean");
Object objSet = beans.get(bean);
String methodName = "set" + name.substring(0, 1).toUpperCase()
+ name.substring(1);
Method m = obj.getClass().getMethod(methodName,
objSet.getClass().getInterfaces()[0]);
m.invoke(obj, objSet);
}
}
}
@Override
public Object getBean(String id) {
return beans.get(id);
}
}
----------
package com.test;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.jdom.JDOMException;
import org.junit.Test;
import com.entry.Student;
import com.service.StudentService;
import com.spring.ioc.ClassPathXmlApplicationContext;
public class TestClass {
@Test
public void testIOC() throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, JDOMException, IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
StudentService service = (StudentService) context.getBean("stuService");
Student student = new Student();
student.setId(1);
student.setName("tt");
service.add(student);
}
}
----------
<beans>
<bean id="stuDao" class="com.dao.StudentDaoImpl" />
<bean id="stuService" class="com.service.StudentServiceImpl" >
<property name="stuDao" bean="stuDao"/>
</bean>
</beans>
模拟 spring IOC
最新推荐文章于 2024-05-24 15:23:12 发布
本文介绍了一个简单的Spring IOC容器实现过程,包括定义DAO层接口及其实现类、Service层接口及其实现类,并通过XML配置文件完成Bean的装配,最终演示了如何通过IOC容器获取并使用Service层对象。
315

被折叠的 条评论
为什么被折叠?



