Spring DI
public class Address {
private String address;
public String getAddress ( ) {
return address;
}
public void setAddress ( String address) {
this . address = address;
}
}
public class Student {
private String name;
private Address address;
private String [ ] books;
private List < String > hobbys;
private Map < String , String > card;
private Set < String > games;
private String wife;
private Properties info;
public void setName ( String name) {
this . name = name;
}
public void setAddress ( Address address) {
this . address = address;
}
public void setBooks ( String [ ] books) {
this . books = books;
}
public void setHobbys ( List < String > hobbys) {
this . hobbys = hobbys;
}
public void setCard ( Map < String , String > card) {
this . card = card;
}
public void setGames ( Set < String > games) {
this . games = games;
}
public void setWife ( String wife) {
this . wife = wife;
}
public void setInfo ( Properties info) {
this . info = info;
}
public void show ( ) {
System . out. println ( "name=" + name
+ ",address=" + address. getAddress ( )
+ ",books="
) ;
for ( String book: books) {
System . out. print ( "<<" + book+ ">>\t" ) ;
}
System . out. println ( "\n爱好:" + hobbys) ;
System . out. println ( "card:" + card) ;
System . out. println ( "games:" + games) ;
System . out. println ( "wife:" + wife) ;
System . out. println ( "info:" + info) ;
}
}
< bean id = " stu" class = " org.example.pojo.Student" >
< property name = " name" value = " lixing" />
< property name = " books" >
< array>
< value> 《西游记》</ value>
< value> 《水浒传》</ value>
< value> 《红楼梦》</ value>
< value> 《三国演义》</ value>
</ array>
</ property>
< property name = " card" >
< map key-type = " java.lang.String" value-type = " java.lang.Integer" >
< entry key = " aaa" value = " 1" />
< entry key = " bbb" value = " 2" />
</ map>
</ property>
< property name = " hobbys" >
< list>
< value> 跑步</ value>
< value> 爬山</ value>
</ list>
</ property>
< property name = " games" >
< set>
< value> 王者荣耀</ value>
< value> 和平精英</ value>
</ set>
</ property>
< property name = " wife" >
< null/>
</ property>
< property name = " address" ref = " addr" />
< property name = " info" >
< props>
< prop key = " 姓名" > 李星</ prop>
< prop key = " 学号" > 19030100010</ prop>
< prop key = " 性别" > 男</ prop>
</ props>
</ property>
</ bean>
< bean id = " addr" class = " org.example.pojo.Address" >
< property name = " address" value = " 河北" />
</ bean>
@Test
public void Studenttest ( ) {
ApplicationContext ac = new ClassPathXmlApplicationContext ( "Beans2.xml" ) ;
Student st = ( Student ) ac. getBean ( "stu" ) ;
st. show ( ) ;
}
xmlns:p="http://www.springframework.org/schema/p"
< bean id = " stu" class = " org.example.pojo.Student" p: name= " LX" p: age= " 22" />
xmlns:c="http://www.springframework.org/schema/c"
< bean id = " stu" class = " org.example.pojo.Student" p: name= " LX" p: age= " 22" />