1、建两个JavaBean
public class Person{
private Long pid;
private String pname;
private Strudent student;
private Set<> sets;
private List<> lists;
private Map<> map;
private Properties properties;
封装……
}
public class Student{
}
2、配置文件 applicationContext-di-set.xml
<bean id="person" class="" >
/** property 用来描述一个类的属性
* 基本数据类型的封装类、String等需要值的类型用value赋值,
* 引用类型用 ref 赋值
*/
<property name="pid" value="1" /> // property 用来描述一个类的属性
<property name="pname" value="aaa" />
<property name="student" >
<ref bean="student">
</property>
<property name="lists" >
<list>
<value>list1</value>
<ref bean="student">
<value>list2</value>
</list>
</property>
<property name="sets" >
<set >
<value>set1</value>
<ref bean="student" />
<value>set2</value>
</set>
</property>
<property name="map" >
<map>
<entry key="m1">
<value>map1</value>
</entry>
<entry key="m2">
<ref bean ="student" />
</entry>
</map>
</property>
<property name="properties" >
<props>
<prop key="prop1">
prop1
</prop>
<prop key="prop2">
prop2
</prop>
</map>
</property>
</bean>
<bean id="student" class="" ></bean>
3、测试
public class DISetTest extends SpringInit{
@Test
public void testSet(){
Person person = (Person)context.getBean("person");
System.out.println(person.getPname());
person.getStudent().student();
}
}
说明:一个类中的属性都可以用springDI的方式进行赋值,但是并不是所有的属性都适合赋值。