标签:
XML 配置
<bean id="person3" class="com.hy.spring.beans.collections.Person">
<property name="name" value="HAO"></property>
<property name="age" value="30"></property>
<property name="cars">
<!-- 使用 list 节点为list 的类型的属性赋值-->
<list>
<ref bean="car"/>
<ref bean="car1"/>
<bean class="com.hy.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
</list>
</property>
</bean>
Person.java
package com.hy.spring.beans.collections;
import java.util.List;
import com.hy.spring.beans.Car;
public class Person {
private String name;
private int age;
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 List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}
XML 配置
<bean id="person4" class="com.hy.spring.beans.collections.NewPerson">
<property name="name" value="Jack"></property>
<property name="age" value="30"></property>
<property name="cars">
<!-- 使用 map 节点及map的entry子 节点配置Map 类型的成员变量-->
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car1"></entry>
<entry key="CC">
<bean class="com.hy.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean>
</entry>
</map>
</property>
</bean>
NewPerson.java
package com.hy.spring.beans.collections;
import java.util.Map;
import com.hy.spring.beans.Car;
public class NewPerson {
private String name;
private int age;
private Map<String,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 Map<String, Car> getCars() {
return cars;
}
public void setCars(Map<String, Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person1 [name=" + name + ", age=" + age + ", cars=" + cars
+ "]";
}
}
XML 配置
<bean id="datasource" class="com.hy.spring.beans.collections.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql://test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
DataSource.java
package com.hy.spring.beans.collections;
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.hy.spring.beans.collections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person3");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("person4");
System.out.println(newPerson);
DataSource dataSource = (DataSource) ctx.getBean("datasource");
System.out.println(dataSource);
}
}
标签:
原文地址:http://www.cnblogs.com/yang-hao/p/5793569.html