
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
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
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>