感谢ITCAST发布的免费视频。
注入其他bean:
方式1:
<bean id=”orderDao” class=”***”/>
<bean id=”orderService” class=”***”>
<property name=”orderDao” ref=”orderDao”></property>
</bean>
方式2:(使用内部bean,但该bean不能被其他bean使用)
<bean id=”orderService” class=”***”>
<property name=”orderDao”>
<bean class=”****”/>
</property>
</bean>
编码剖析注入基本类型原理:
package junit.test;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class MyClassPathXMLApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public MyClassPathXMLApplicationContext(String fileName) {
this.readXML(fileName);
this.instanceBeans();
this.injectObject();
}
private void injectObject() {
// TODO Auto-generated method stub
for (BeanDefinition beanDefinition : beanDefines) {
Object bean = sigletons.get(beanDefinition.getId());
if (bean != null) {
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for (PropertyDefinition properties : beanDefinition.getProperties()) {
for (PropertyDescriptor properDesc : ps) {
if (properties.getName().equals(properDesc.getName())) {
Method setter = properDesc.getWriteMethod(); //get the setter method
if (setter != null) {
Object value = null;
if (properties.getRef() != null && !properties.getRef().equals("")) {
value = sigletons.get(properties.getRef());
} else {
value = ConvertUtils.convert(properties.getValue(), properDesc.getPropertyType());
}
setter.setAccessible(true);
setter.invoke(bean, value); //set ref object to the property
}
break;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//List<PropertyDefinition> properties =
}
private void instanceBeans() {
// TODO Auto-generated method stub
for (BeanDefinition beanDefinition : beanDefines) {
try {
if (beanDefinition.getClassName() != null && !beanDefinition.getClassName().equals("")) {
sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void readXML(String fileName) {
// TODO Auto-generated method stub
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader().getResource(fileName);
Map<String, String> nsMap = new HashMap<String, String>();
XPath xsub = null;
List<Element> beans = null;
document = saxReader.read(xmlpath);
nsMap.put("ns", "http://www.springframework.org/schema/beans"); //add namespace
xsub = document.createXPath("//ns:beans/ns:bean"); //create beans/bean query path
xsub.setNamespaceURIs(nsMap); //set namespace
beans = xsub.selectNodes(document); //get nodes of the document
for (Element element : beans) {
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
XPath propertySub = element.createXPath("ns:property");
List<Element> properties = null;
propertySub.setNamespaceURIs(nsMap);
properties = propertySub.selectNodes(element);
for (Element property : properties) {
String propertyName = property.attributeValue("name");
String propertyRef = property.attributeValue("ref");
String propertyValue = property.attributeValue("value");
PropertyDefinition pro = new PropertyDefinition();
pro.setName(propertyName);
pro.setRef(propertyRef);
pro.setValue(propertyValue);
beanDefine.getProperties().add(pro);
}
beanDefines.add(beanDefine);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String beanName) {
return this.sigletons.get(beanName);
}
}