码迷,mamicode.com
首页 > 其他好文 > 详细

集合属性

时间:2016-08-21 21:17:37      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!