1.bean 与 bean之间的关系是Spring的web.xml来维护的。
<bean id="userService" class="com.spring.study.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
<!--
<property name="userDaoAA" ref="userDao" />
public void setUserDaoAA(UserDao userDao) {this.userDao = userDao;} -->
</bean>
<bean id="userDao" class="com.spring.study.dao.impl.UserDaoImpl" />
2.自己实现的Spring bean 与bean之间的关系注入
public class CnpcClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<>();
// [com.spring.framework.BeanDefinition@589af639, com.spring.framework.BeanDefinition@259b45a1]
private Map<String, Object> singletons = new HashMap<>();
//{userService=com.spring.study.service.impl.UserServiceImpl@6eb1054b, userDao=com.spring.study.dao.impl.UserDaoImpl@27d2b7b3}
// beanId -- beanClass对象
public CnpcClassPathXmlApplicationContext(String fileName) {
this.readXML(fileName);
this.instanceBeans();
this.injectObject();
}
/**
* 注入property属性,构造bean与bean的关联关系
*/
private void injectObject() {
for (BeanDefinition beanDefinition : beanDefines) { // 循环所有的bean
Object bean = singletons.get(beanDefinition.getId());
//com.spring.study.service.impl.UserServiceImpl@47ca5c4f
// 取得实例化后的bean对象
if (bean != null) {
try {
<strong>PropertyDescriptor[] ps = Introspector.getBeanInfo(
bean.getClass()).getPropertyDescriptors()</strong>;
for (PropertyDefinition propertyDefinition : beanDefinition
//java.beans.PropertyDescriptor[name=class; propertyType=class java.lang.Class; readMethod=public final native java.lang.Class java.lang.Object.getClass()]
//java.beans.PropertyDescriptor[name=userDao; propertyType=interface com.spring.study.dao.UserDao; readMethod=public com.spring.study.dao.UserDao com.spring.study.service.impl.UserServiceImpl.getUserDao(); writeMethod=public void com.spring.study.service.impl.UserServiceImpl.setUserDao(com.spring.study.dao.UserDao)]
.getPropertys()) { //获取bean下面的property
for (PropertyDescriptor properdesc : ps) {
System.out.println(properdesc.getName());
//class
//userDao
if (propertyDefinition.getName().equals(
properdesc.getName())) {
//com.spring.framework.PropertyDefinition@71f80d51 name ref userDao
//java.beans.PropertyDescriptor[name=userDao; propertyType=interface com.spring.study.dao.UserDao; readMethod=public com.spring.study.dao.UserDao com.spring.study.service.impl.UserServiceImpl.getUserDao(); writeMethod=public void com.spring.study.service.impl.UserServiceImpl.setUserDao(com.spring.study.dao.UserDao)]
<strong>Method setter = properdesc.getWriteMethod();</strong> // 获取属性的setter方法
if (setter != null) {
Object value = singletons
.get(propertyDefinition.getRef());
setter.setAccessible(true); // private
<strong>setter.invoke(bean, value);</strong> // 把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
for (BeanDefinition beanDefine : beanDefines) {
if (beanDefine != null && !"".equals(beanDefine.getClassName()))
try {
singletons.put(beanDefine.getId(),
Class.forName(beanDefine.getClassName())
.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取xml配置文件
*
* @param fileName
*/
@SuppressWarnings("unchecked")
private void readXML(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader()
.getResource(fileName);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<>();
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节点
for (Element element : beans) {
String id = element.attributeValue("id");// 获取id属性值
String clazz = element.attributeValue("class");// 获取class属性值
BeanDefinition beanDefintion = new BeanDefinition(id, clazz);
XPath propertysub = document.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap);
List<Element> propertys = propertysub.selectNodes(element);
for (Element propertyElement : propertys) {
String name = propertyElement.attributeValue("name");
String ref = propertyElement.attributeValue("ref");
// System.out.println(name + "--" + ref);
PropertyDefinition propertyDefintion = new PropertyDefinition(
name, ref);
beanDefintion.getPropertys().add(propertyDefintion);
}
beanDefines.add(beanDefintion);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String beanName) {
return singletons.get(beanName);
}
}
public class PropertyDefinition {
private String name;
private String ref;
public PropertyDefinition() {
}
public PropertyDefinition(String name, String ref) {
this.name = name;
this.ref = ref;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
}
public class BeanDefinition {
private String id;
private String className;
private List<PropertyDefinition> propertys = new ArrayList<>();
public BeanDefinition() {
}
public BeanDefinition(String id, String className) {
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<PropertyDefinition> getPropertys() {
return propertys;
}
public void setPropertys(List<PropertyDefinition> propertys) {
this.propertys = propertys;
}
}