Spring教程 - 弹簧列表属性
Spring教程 - 弹簧列表属性
我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。
我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。...
Java Bean
为了展示如何使用xml配置文件来填充集合属性,我们定义了一个具有四个集合属性的Customer对象。
package com.www.w3cschool.cnmon;
import java.util.ArrayList;
import java.util.List;
public class Customer
{
private List<Object> lists = new ArrayList<Object>();
public String toString(){
return lists.toString();
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
}
Person Java Bean
package com.www.w3cschool.cnmon;
public class Person {
private String name;
private int age;
private String address;
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]";
}
}
列表
以下代码显示如何将数据填充到java.util.List类型化属性。
代码填充三个值。 第一个是硬编码值1.第二个是一个bean参考。 我们必须在某处定义PersonBean,以便在此处使用它。 第三个是bean定义与属性设置。
...
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.www.w3cschool.cnmon.Person">
<property name="name" value="java2sList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
...
例子
Full Spring的bean配置文件。
<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-2.5.xsd">
<bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.www.w3cschool.cnmon.Person">
<property name="name" value="java2sList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
</bean>
<bean id="PersonBean" class="com.www.w3cschool.cnmon.Person">
<property name="name" value="java2s1" />
<property name="address" value="address 1" />
<property name="age" value="28" />
</bean>
</beans>
下面是加载和运行配置的代码。
package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}
输出

Download Java2s_Spring_List_Properties.zip
ListFactoryBean
ListFactoryBean类可以使用ArrayList或创建一个具体的List集合类LinkedList在Spring的bean配置文件中,然后我们可以注入创建的列表对象到我们的Java bean。
这里是Java bean类。
package com.www.w3cschool.cnmon;
import java.util.ArrayList;
import java.util.List;
public class Customer
{
private List<Object> lists = new ArrayList<Object>();
public String toString(){
return lists.toString();
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
}
这里是Spring的bean配置文件。
<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-2.5.xsd">
//from w w w. ja va 2 s.com
<bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
<property name="lists">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</property>
</bean>
</beans>
Download Java2s_Spring_ListFactoryBean.zip
util模式
我们还可以使用util模式和< util:list> 以将数据填充到列表中。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
<property name="lists">
<util:list list-class="java.util.ArrayList">
<value>1</value>
<value>2</value>
<value>3</value>
</util:list>
</property>
</bean>
</beans>
Download Java2s_Spring_util_List.zip
我们还可以使用util模式和< util:list> 以将数据填充到列表中。...
package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"SpringBeans.xml");
Customer cust = (Customer) context.getBean("CustomerBean");
System.out.println(cust);
}
}