您的位置:首页 > 编程语言 > Java开发

spring setter方法依赖注入(DI)原理

2013-05-20 20:31 393 查看
使用setter方法DI:

beans.xml

<bean id="beanDao" class="cn.transaction.BeanDao"></bean>

<bean id="bean1" class="cn.transaction.Bean1">

<property name="beanDao" ref="beanDao"></property>

</bean>

Bean1.java

public class Bean1 {

private BeanDao beanDao;

public BeanDao getBeanDao() {

return beanDao;

}

public void setBeanDao(BeanDao beanDao) {

this.beanDao = beanDao;

}

public void save(){

beanDao.save();

}

}

PropertyDefinition.java

public class PropertyDefinition {

private String name;

private String ref;

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;

}

}

BeanDefinition.java

public class BeanDefinition {

private String id;

private String className;

private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();

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;

}

}

MyClassPathXmlApplicationContext.java

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();

}

/**

* 读取xml配置文件

* @param filename

*/

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

for(Element element: beans){

String id = element.attributeValue("id");//获取id属性值

String clazz = element.attributeValue("class"); //获取class属性值

BeanDefinition beanDefine = new BeanDefinition(id, 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");

String propertyref = property.attributeValue("ref");

PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref);

beanDefine.getPropertys().add(propertyDefinition);

}

beanDefines.add(beanDefine);

}

}catch(Exception e){

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

e.printStackTrace();

}

}

}

/**

* 获取bean实例

* @param beanName

* @return

*/

public Object getBean(String beanName){

return this.sigletons.get(beanName);

}

/**

* 为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) {

}

}

}

}

}

Test

MyClassPathXmlApplicationContext mcxt = new MyClassPathXmlApplicationContext("beans.xml");

Bean1 bean1 = (Bean1) mcxt.getBean("bean1");

bean1.save();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: