模拟spring框架注入实现原理 (五)

2014-11-24 10:33:25 · 作者: · 浏览: 4

*/
private void injectObject() {
for(BeanInformation beanInformation : beansInformation){
Object bean = singleton.get(beanInformation.getId());
if(bean!=null){
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyInformation propertyInformation : beanInformation.getPropertyInformation()){
for(PropertyDescriptor properdesc : ps){
if(propertyInformation.getName().equals(properdesc.getName())){
Method setter = properdesc.getWriteMethod();//获取属性的setter方法 ,private
if(setter!=null){
Object value = null;
if(propertyInformation.getRef()!=null && !"".equals(propertyInformation.getRef().trim())){
value = singleton.get(propertyInformation.getRef());
}else{
value = ConvertUtils.convert(propertyInformation.getValue(), properdesc.getPropertyType());
}
setter.setAccessible(true);
setter.invoke(bean, value);//把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
}
}
}
}

public Object getBean(String id){
return this.singleton.get(id);
}
}
[java]
package junit.test;

import java.util.HashSet;
import java.util.Set;

public class BeanInformation {
private String id;
private String name;
private Set propertyInformation = new HashSet();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getPropertyInformation() {
return propertyInformation;
}
public void setPropertyInformation(Set propertyInformation) {
this.propertyInformation = propertyInformation;
}
}

package junit.test;

import java.util.HashSet;
import java.util.Set;

public class BeanInformation {
private String id;
private String name;
private Set propertyInformation = new HashSet();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getPropertyInformation() {
return propertyInformation;
}
public void setPropertyInformation(Set propertyInformation) {
this.propertyInformation = propertyInformation;
}
}
[java]
package junit.test;

public class PropertyInformation {
private String name;
private String ref;
private String value;
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 String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

package junit.test;

public class PropertyInformation {
private String name;
private String ref;
private String value;
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;
}
publi