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

spring bean setter属性注入

2015-11-13 17:09 597 查看
quote: http://outofmemory.cn/java/spring/spring-DI-via-setter-property-method  

我们可以方便的通过构造函数来注入spring bean,也可以通过setter属性来做spring bean的注入。

注入简单类型的属性

一个简单的示例,我们给Person类定义age和name两个属性,然后在spring配置文件中通过属性注入值。

Person类的定义如下:
package cn.outofmemory.spring;

public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}


Person类是一个纯粹的pojo类,而且目前他的两个属性都是简单类型,我们看下如何在spring配置文件中注入属性。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.outofmemory.spring.Person">
<property name="name" value="John"/>
<property name="age" value="20"/>
</bean>
</beans>


我们通过property节点来设置属性的值,name是属性的名字,注意不需要包含set,value是要设定的值。对于简单类型可以通过value来设置值,而对于复杂类型可以通过在property节点中嵌套bean节点来实现,也可以通过ref来引用已经定义好的bean

下面我们给Person类增加一个复杂类型的属性:

private ContactInfo contact;

public ContactInfo getContact() {
return contact;
}
public void setContact(ContactInfo contact) {
this.contact = contact;
}


其中ContactInfo也是一个Pojo类,他的定义如下:
package cn.outofmemory.spring;

public class ContactInfo {
private String address;
private String zip;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}


我们要给spring配置文件中的bean配置这个复杂的属性有两种方式,分别如下:

内置bean方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.outofmemory.spring.Person">
<property name="name" value="John"/>
<property name="age" value="20"/>
<property name="contact">
<bean class="cn.outofmemory.ContactInfo">
<property name="address" value="Street 5th"/>
<property name="zip" value="9999"/>
</bean>
</property>
</bean>
</beans>

 使用property的ref属性方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.outofmemory.ContactInfo" id="concat">
<property name="address" value="Street 5th" />
<property name="zip" value="9999" />
</bean>
<bean class="cn.outofmemory.spring.Person">
<property name="name" value="John" />
<property name="age" value="20" />
<property name="contact" ref="concat" />
</bean>
</beans>

给spring bean注入List类型的属性:

我们给Person类添加下面的新属性,新属性的类型是泛型的String:
private List<String> children;

public List<String> getChildren() {
return children;
}
public void setChildren(List<String> children) {
this.children = children;
}


我们看下如何在spring配置文件中注入此list属性:
<bean class="cn.outofmemory.spring.Person">
<property name="children">
<list>
<value>James</value>
<value>Mary</value>
</list>
</property>
</bean>

同样可以给bean注入数组类型的属性

我们给Person类添加contacts数组,如下代码:
private ContactInfo[] contacts;

public ContactInfo[] getContacts() {
return contacts;
}
public void setContacts(ContactInfo[] contacts) {
this.contacts = contacts;
}


contacts属性的类型是ContactInfo类型的数组。在spring配置文件中可以用list类似的方式来注入,如下配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.outofmemory.ContactInfo" id="contact">
<property name="address" value="Street 5th" />
<property name="zip" value="9999" />
</bean>
<bean class="cn.outofmemory.spring.Person">
<property name="name" value="John" />
<property name="age" value="20" />
<property name="contact" ref="concat" />
<property name="children">
<list>
<value>James</value>
<value>Mary</value>
</list>
</property>
<property name="contacts">
<list>
<bean class="cn.outofmemory.ContactInfo">
<property name="address" value="Str. 100th"/>
<property name="zip" value="9999"/>
</bean>
<ref bean="contact"/>
</list>
</property>
</bean>
</beans>


注意contacts属性的第一个元素是一个bean节点,而第二个元素是ref节点,这里的ref需要指定bean属性,bean属性指向上面已经定义好的contact bean。

在spring配置文件中注入Map类型的属性

我们再给Person类添加一个Map类型的属性:
private Map<String,Object> kvs;

public Map<String, Object> getKvs() {
return kvs;
}
public void setKvs(Map<String, Object> kvs) {
this.kvs = kvs;
}


map类型在spring配置文件中可以这样来配置:
<property name="kvs">
<map>
<entry key="key1" value="key2"></entry>
<entry key="key2">
<bean class="cn.outofmemory.ContactInfo">
<property name="address" value="Str. 100th"/>
<property name="zip" value="9999"/>
</bean>
</entry>
<entry key="key3" value-ref="contact"/>
</map>
</property>


kvs属性节点内,首先是map节点,在map节点内是一系列的entry节点,这里有三个entry节点,第一个节点的key和value值都是简单的String类型,而第二个节点的值是内置的bean节点指定的ContactInfo,而第三个节点是使用value-ref属性指定的另外一个bean节点。

在Spring配置文件中指定Set类型的属性

我们首先给Person类添加一个Set类型的属性,如下:
private Set<String> rings;

public Set<String> getRings() {
return rings;
}
public void setRings(Set<String> rings) {
this.rings = rings;
}


可以通过如下配置给bean添加Set类型的属性:
<property name="rings">
<set>
<value>RingA</value>
<value>RingB</value>
</set>
</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring bean