Spring (三)注入依赖对象、注入集合类型的属性

时间:2020-12-09 17:01:29

1、 注入依赖对象
a. 基本类型对象注入:

<bean id="serviceDao" class="ServiceDaoBean" >
<constructor-arg index="0" type="java.lang.String" /> // 构造器注入
<property name="name" value="zhao" /> // 属性setter方法注入
</bean>

b. 注入其他bean
方式1:

<bean id="serviceDao" class="ServiceDaoBean" ></bean>
<bean id="service" class="ServiceBean" >
<property name="serviceDao" ref="serviceDao" />
</bean>
方式2:
<bean id="service" class="ServiceBean" >
<property name="serviceDao" >
<bean class="ServiceDaoBean" />
</property>
</bean>

c. 注入基本属性类型

<bean id="service" class="ServiceBean" >
<property name="name" value="zhangsan" />
<property name="id" value="88" />
</bean>
原理如下:
 private void readXml(String filename) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
URL xmlPath = this.getClass().getClassLoader().getResource(filename); // 可以获取bin目录下的资源文件
doc = reader.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");
// 设置命名空间
xsub.setNamespaceURIs(nsMap);
// 获取文档下所有的bean
List<Element> beans = xsub.selectNodes(document);
for (Element element : beans) {
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
BeanDefinition d = new BeanDefinition(id, clazz);
XPath propertySub = element.createXPath("ns:property");
propertySub.setNamespaceURIs(nsMap);
List<Element> propertyBeans = propertySub.selectNodes(element);
for ( Element propE : propertyBeans ) {
String pName = propE.attributeValue("name");
String pRef = propE.attributeValue("ref");
String value = propE.attributeValue("value");
PropertyDefinition pd = new PropertyDefinition(pName, pRef, value);
bds.getProperties().add(pd);
}

bds.add(d);
}
} catch (Exception e) {
e.printStackTrace();
}
}


private void injuectObject() {
for ( BeanDefinition beanDefinition : beanDefinition ) {
Object bean = sigletons.get(beanDefinition.getId());
if ( bean != null ) {
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for ( PropertyDefinition propertyDefinition : beanDefinition.getProperties() ) {
for ( PropertyDescriptor propertyDesc : ps ) {
if ( propertyDefinition.getName().equals(propertyDesc.getName())) {
// 获取属性的setter方法
Method setter = propertyDesc.getWriteMethod();
if ( setter != null ) {
Object value = null;
if ( propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim()) {
value = sigletons.get(propertyDefinition.getRef());
} else {
// 需要引入commons-beanutils-core.jar包
value = ConvertUtils.convert(propertyDefinition.getValue(), propertyDesc.getPropertyType());
}

setter.setAccessible(true); // 如果setter为private
setter.invoke(bean, value); //把引用注入到属性
}
}
}
}
} catch (Exception e ) {
// TODO
}
}
}
}

2、 注入集合类型的属性

public class PersonServiceBean {
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();

// set和get方法必须有
}

applicationContext.xml

<bean id="personService" class="PersonServiceBean">
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1">第一个</entry>
<entry key="key-2" value="value-2">第一个</entry>
<entry key="key-3" value="value-3">第一个</entry>
</map>
</property>
</bean>