标签:bsp ati val coding 集合属性 encoding code double his
<?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="car1" class="com.gong.spring.beans.Car"> <property name="name" value="baoma"></property> </bean> <bean id="car2" class="com.gong.spring.beans.Car"> <property name="name" value="benchi"></property> </bean> <bean id="car3" class="com.gong.spring.beans.Car"> <property name="name" value="binli"></property> </bean> <bean id="student" class="com.gong.spring.beans.Student"> <property name="name" value="tom"></property> <property name="age" value="12"></property> <property name="score" value="98.00"></property> <property name="cars"> <!-- 使用List节点为List集合属性赋值 <list> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </list> </property> </bean> </beans>
这种情况下,是在一个bean的里面进行配置的,假设现在我们有另外一个bean,也需要使用List集合里的bean,那么应该怎么做呢?
首先,引入命名空间util,在applicationContext.xml下方的namespace里面,勾选下即可。如果没有,则需要安装Spring Tool Suite(STS) for Eclipse,具体装法可百度。
然后配置一个公用集合bean:
<util:list id="cars"> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </util:list>
此时可去掉上述代码中的红色部分,并这样使用:
<property name="cars" ref="cars">
补充其它代码:
Car.java
package com.gong.spring.beans; public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car [name=" + name + "]"; } }
Student.java
package com.gong.spring.beans; import java.util.List; import java.util.Map; public class Student { private String name; private int age; private double score; private List<Car> cars; 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 double getScore() { return score; } public void setScore(double score) { this.score = score; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]"; } }
Main.java
package com.gong.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从容器中获取Bean实例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); } }
输出:
顺便看一下util中都有什么:
对于不同的集合,按照其相应的语法进行配置即可。
spring之配置单例的集合bean,以供多个bean进行引用
标签:bsp ati val coding 集合属性 encoding code double his
原文地址:https://www.cnblogs.com/xiximayou/p/12150343.html