ns:bean里面把冒号写成了顿号,真是太不仔细了。
自定义容器
/**
* 实现的spring容器
*
* @author Administrator
*
*/
public class ItcastClassPathXMLApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public ItcastClassPathXMLApplicationContext() {
}
public ItcastClassPathXMLApplicationContext(String filename) {
// System.out.println("构造方法 ");
this.readXml(filename);// 调用 读取配置文件 的方法
this.instanceBeans();// 调用bean的实例化
this.injectObject();// 注入对象
}
/**
* 为bean对象的属性注入值
*/
private void injectObject() {
for (BeanDefinition beanDefinition : beanDefines) {
Object bean = sigletons.get(beanDefinition.getId());
if (bean != null) {
// 取得属性描述 ,是一个数组
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(
bean.getClass()).getPropertyDescriptors();
for (PropertyDefinition propertyDefinition : beanDefinition
.getPropertys()) {// 取所有属性
for (PropertyDescriptor properdesc : ps) {
if (propertyDefinition.getName().equals(
properdesc.getName())) {
Method setter = properdesc.getWriteMethod();// 获取属性的setter方法.
// private
if (setter != null) {
Object value = sigletons
.get(propertyDefinition.getRef());
setter.setAccessible(true);// 设置为可访问
setter.invoke(bean, value);// 把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
// System.out.println("bean实例化方法被调用");
// 利用反射机制把bean实例化
for (BeanDefinition beanDefinition : beanDefines) {
try {
// 判断BeanDefinition的实例获得的类名不为null和空串
if (beanDefinition.getClassName() != null
&& !"".equals(beanDefinition.getClassName().trim()))
sigletons.put(beanDefinition.getId(), Class.forName(
beanDefinition.getClassName()).newInstance());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 读取配置文件信息
*
* @param filename
*/
private void readXml(String filename) {
// System.out.println("读取xml文件的方法被调用了");
SAXReader saxReader = new SAXReader();// 创建读取器
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader()
.getResource(filename);//取得当前xml文件在本地的位置
document = saxReader.read(xmlpath);// 读取路径
System.out.println(document);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> beans = xsub.selectNodes(document);// 获取文档下所有bean节点
System.out.println(beans.size());
for (Element element : beans) {
String id = element.attributeValue("id");// 获取id属性值
String clazz = element.attributeValue("class");// 获取class属性值
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
System.out.println("id=" + id);
System.out.println("clazz=" + clazz);
XPath propertysub = element.createXPath("ns:property");// 船舰查询路径
propertysub.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> propertys = propertysub.selectNodes(element);// 查找节点
for (Element property : propertys) {
String propertyName = property.attributeValue("name");// 取得property的name值
String propertyref = property.attributeValue("ref");// 取得property的ref值
System.out.println(propertyName + "= " + propertyref);
PropertyDefinition propertyDefinition = new PropertyDefinition(
propertyName, propertyref);
beanDefine.getPropertys().add(propertyDefinition);// 将属性对象加入到bean中
}
beanDefines.add(beanDefine);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取bean 实例
*
* @param beanName
* @return
*/
public Object getBean(String beanName) {
return this.sigletons.get(beanName);
}
}
bean.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<bean id="personService"
class="cn.itcast.service.impl.PersonServiceBean">
<property name="IPersonDao" ref="personDaoBean"></property>
</bean>
<bean id="personDaoBean" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
</beans>
自定义属性类 PropertyDefinition.java
package junit.test;
public class PropertyDefinition {
private String name;
private String ref;
public PropertyDefinition(String name, String ref) {
this.name = name;
this.ref = ref;
}
getter&setter method
}
测试类:springTest
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import cn.itcast.service.IPersonService;
import cn.itcast.service.impl.PersonServiceBean;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
// ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
// "beans.xml");
ItcastClassPathXMLApplicationContext ctx=new ItcastClassPathXMLApplicationContext("beans.xml");
IPersonService ipersonService = (IPersonService)ctx
.getBean("personService");//调用自定义容器的getBean方法
ipersonService.Save();
// ctx.close();
// ctx.registerShutdownHook();
}
}
自定义bean类:
package junit.test;
import java.util.ArrayList;
import java.util.List;
public class BeanDefinition {
private String id;
private String className;
private List<PropertyDefinition> propertys=new ArrayList<PropertyDefinition>();
生成getter,setter方法
}
package cn.itcast.dao.impl;
import cn.itcast.dao.IPersonDao;
public class PersonDaoBean implements IPersonDao {
public void add(){
System.out.println("这是personDaoBean的Add()方法");
}
}
package cn.itcast.service;
public interface IPersonService {
public abstract void Save();
}
package cn.itcast.service.impl;
import cn.itcast.dao.IPersonDao;
import cn.itcast.service.IPersonService;
/**
* 业务bean
* @author Administrator
*
*/
public class PersonServiceBean implements IPersonService {
private IPersonDao iPersonDao;
public IPersonDao getIPersonDao() {
return iPersonDao;
}
public void setIPersonDao(IPersonDao personDao) {
iPersonDao = personDao;
}
public void Save(){
iPersonDao.add();
}
}
运行测试类
out:
这是personDaoBean的Add()方法