集合实体类
public class CollectionDemo {
private List list;
private Set set;
private Map map;
private Properties prop;
Car实体类
public class Car {
private String name;
private double price;
Person实体类
public class Person {
private String name;
private Car car;
applicationContext.xml
< bean name = " car" class = " di.Car" >
< constructor-arg index = " 0" type = " java.lang.String" value = " 奥迪" > </ constructor-arg>
< constructor-arg index = " 1" type = " double" value = " 400000" > </ constructor-arg>
</ bean>
< bean name = " car1" class = " di.Car" >
< property name = " name" value = " 宝马" > </ property>
< property name = " price" value = " 500000" > </ property>
</ bean>
< bean name = " person" class = " di.Person" >
< property name = " name" value = " 张三" > </ property>
< property name = " car" ref = " car1" > </ property>
</ bean>
< bean id = " collectionDemo" class = " di.CollectionDemo" >
< property name = " list" >
< list>
< value> 张三</ value>
< value> 10</ value>
< ref bean = " car" />
</ list>
</ property>
< property name = " set" >
< set>
< value> 李四</ value>
< value> 20</ value>
< ref bean = " person" />
</ set>
</ property>
< property name = " map" >
< map>
< entry key = " username" value = " 李四" > </ entry>
< entry key-ref = " person" value-ref = " car" > </ entry>
</ map>
</ property>
< property name = " prop" >
< props>
< prop key = " company" > yl</ prop>
< prop key = " price" > 10000000</ prop>
</ props>
</ property>
</ bean>
DiTest
@Test
public void test1 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
Car car = ( Car) applicationContext. getBean ( "car" ) ;
System. out. println ( car. getName ( ) + " " + car. getPrice ( ) ) ;
}
@Test
public void test2 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
Car car = ( Car) applicationContext. getBean ( "car1" ) ;
System. out. println ( car. getName ( ) + " " + car. getPrice ( ) ) ;
}
@Test
public void test3 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
Person person = ( Person) applicationContext. getBean ( "person" ) ;
System. out. println ( person. getName ( ) + " " + person. getCar ( ) . getName ( ) ) ;
}
@Test
public void test4 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
CollectionDemo cd = ( CollectionDemo) applicationContext. getBean ( "collectionDemo" ) ;
System. out. println ( cd. getList ( ) ) ;
}
@Test
public void test5 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
CollectionDemo cd = ( CollectionDemo) applicationContext. getBean ( "collectionDemo" ) ;
System. out. println ( cd. getSet ( ) ) ;
}
@Test
public void test6 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
CollectionDemo cd = ( CollectionDemo) applicationContext. getBean ( "collectionDemo" ) ;
System. out. println ( cd. getMap ( ) ) ;
}
@Test
public void test7 ( ) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
CollectionDemo cd = ( CollectionDemo) applicationContext. getBean ( "collectionDemo" ) ;
System. out. println ( cd. getProp ( ) ) ;
}
p/c命名空间
< bean id = " dog" class = " pc.Dog" p: name= " 小白" p: color= " 白色" > </ bean>
< bean id = " dog1" class = " pc.Dog" c: name= " 小黑" c: color= " 黑色" > </ bean>
< bean id = " person" class = " pc.Person" p: name= " 小王" p: dog-ref= " dog1" p: age= " 20" > </ bean>
< bean id = " person1" class = " pc.Person" >
< property name = " name" value = " #{person.name}" > </ property>
< property name = " dog" value = " #{dog1}" > </ property>
< property name = " age" value = " #{person.age+10}" > </ property>
</ bean>