1:通过属性的setter方法注入
写类PersonDaoBean
public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("z执行PersonDaoBean中的方法");
}
}
在要使用这个类的类中使用setter()
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
在app*.xml文件中,首先创建这个类的Bean,然后把这个bean设置为使用这个类所对应的bean的属性
<bean id="personDao" class="com.dao.impl.PersonDaoBean" ></bean>
<bean id="personService" class="com.service.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></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.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class ClassPathXMLApplicationContext {
//用来存放bean的信息
private List<BeanDefinition> BeanDefines = new ArrayList<BeanDefinition>();
//保存bean
private Map<String ,Object > sigletons =new HashMap<String, Object>();
public ClassPathXMLApplicationContext(String fileName){
this.readXML(fileName);
this.instanceBeans();
this.injectObject();
}
//为bean对象的属性注入值
private void injectObject() {
for (BeanDefinition beanDefinition :BeanDefines) {
//实例化过后的bean
Object bean = sigletons.get(beanDefinition.getId());
if(bean!=null){
try {
PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDefinition propertyDefinition:beanDefinition.getProperties()){
for(PropertyDescriptor properdesc:ps){
if(propertyDefinition.getName().equals(properdesc.getName())){
Method setter =properdesc.getWriteMethod();//获取setter方法
if(setter!=null){
Object value =sigletons.get(propertyDefinition.getRef());
setter.setAccessible(true);//允许调用private的方法
setter.invoke(bean, value);//把应用对象注入到属性
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//完成bean的实例化
private void instanceBeans() {
for(BeanDefinition beanDefinition:BeanDefines){
try {
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();
}
}
}
//读取app*.xml里面的内容
private void readXML(String fileName) {
SAXReader saxReader = new SAXReader();
Document document =null;
try{
URL xmlPath=this.getClass().getClassLoader().getResource(fileName);
//System.out.println(xmlPath);file:/G:/workspaces1/spring/bin/beans.xml
document =saxReader.read(xmlPath);
Map<String,String> nsMap=new HashMap<String, String>();
//加入命名空间
nsMap.put("ns","http://www.springframework.org/schema/beans");
//创建beans/bean的查询路径
XPath xsub = document.createXPath("//ns:beans/ns:bean");
//System.out.println(xsub);[XPath: /descendant-or-self::node()/child::ns:beans/child::ns:bean]
//设置命名空间
xsub.setNamespaceURIs(nsMap);
//获取文档下的所有的bean节点
List<Element> beans = xsub.selectNodes(document);
for (Element element : beans) {
String id =element.attributeValue("id");//获取id属性值
String clazz = element.attributeValue("class");//获取class属性值
BeanDefinition beanDefine =new BeanDefinition(id, clazz);
//得到bean下面的所有property节点
XPath propertyPath = document.createXPath("ns:property");
propertyPath.setNamespaceURIs(nsMap);
List<Element> properties =propertyPath.selectNodes(element);
for(Element property:properties){
String propertyName = property.attributeValue("name");
String propertyRef = property.attributeValue("ref");
System.out.println(propertyName+"="+propertyRef);
PropertyDefinition propertyDefinition =new PropertyDefinition(propertyName, propertyRef);
beanDefine.getProperties().add(propertyDefinition);
}
BeanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
//获取bean实例
public Object getBean(String beanName){
return this.sigletons.get(beanName);
}
}