1、配置List属性
<!-- 配置List属性 --> <bean id="person4" class="com.broadtext.beans.collection.Person"> <property name="name" value="hjj"></property> <property name="age" value="24"></property> <property name="cars"> <!-- 使用list节点为List类型属性值赋值 --> <list> <ref bean="car"/> <ref bean="car2"/> <bean class="com.broadtext.beans.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Shanghai"></constructor-arg> <constructor-arg type="double" value="300000"></constructor-arg> </bean> </list> </property> </bean>
2、配置Map属性
<!-- 配置Map属性 --> <bean id="newPerson" class="com.broadtext.beans.collection.NewPerson"> <property name="name" value="hjjj"></property> <property name="age" value="28"></property> <property name="cars"> <map> <entry key="A_Car" value-ref="car"></entry> <entry key="B_Car" value-ref="car2"></entry> </map> </property> </bean>
3、可以使用util命名空间单独配置一个集合bean以供其他bean应用
<!-- 单独配置一个集合bean以供其他bean应用 --> <util:list id="cars"> <ref bean="car" /> </util:list>
<!-- 配置List属性 --> <bean id="person4" class="com.broadtext.beans.collection.Person"> <property name="name" value="hjj"></property> <property name="age" value="24"></property> <!-- <property name="cars"> 使用list节点为List类型属性值赋值 <list> <ref bean="car"/> <ref bean="car2"/> <bean class="com.broadtext.beans.Car"> <constructor-arg value="Ford"></constructor-arg> <constructor-arg value="Shanghai"></constructor-arg> <constructor-arg type="double" value="300000"></constructor-arg> </bean> </list> </property> --> <property name="cars" ref="cars"></property> </bean>
4、模拟配置properties
<!-- 模拟配置properties --> <bean id="dataSource" class="com.broadtext.beans.collection.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">12345</prop> <prop key="jdbcUrl">jdbc:mysql://test</prop> <prop key="driverClass">com.jdbc.mysql</prop> </props> </property> </bean>
5、Person.java、NewPerson.java、DataSource.java、Main.java
Person.java
package com.broadtext.beans.collection; import java.util.List; import com.broadtext.beans.Car; public class Person { private String name; private int age; private List<Car> cars; public Person(String name, int age, List<Car> cars) { super(); this.name = name; this.age = age; this.cars = cars; } public Person() { } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", cars=" + 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 List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } }
NewPerson.java
package com.broadtext.beans.collection; import java.util.Map; import com.broadtext.beans.Car; public class NewPerson { private String name; private int age; private Map<String, Car> cars; public NewPerson(String name, int age, Map<String, Car> cars) { super(); this.name = name; this.age = age; this.cars = cars; } public NewPerson() { } @Override public String toString() { return "NewPerson [name=" + name + ", age=" + age + ", cars=" + 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 Map<String, Car> getCars() { return cars; } public void setCars(Map<String, Car> cars) { this.cars = cars; } }
DataSource.java
package com.broadtext.beans.collection; import java.util.Properties; public class DataSource { private Properties properties; public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "DataSource [properties=" + properties + "]"; } }
Main.java
package com.broadtext.beans.collection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * <p> * Description: 测试bean的List属性、Map属性的配置,模拟配置properties * </p> */ public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person) ctx.getBean("person4"); System.out.println(person); NewPerson newPerson = (NewPerson) ctx.getBean("newPerson"); System.out.println(newPerson); DataSource dataSource = ctx.getBean(DataSource.class); System.out.println(dataSource.getProperties()); } }
时间: 2024-10-16 12:02:43