Spring学习笔记(三)

C、集合的装配
public   class   SomeBean  {
private  List  listProperty ;
private  Set  setProperty ;
private  Map  mapProperty ;
private  Properties  property ;
public  List getListProperty() {
return   listProperty ;
}
public   void  setListProperty(List listProperty) {
this . listProperty  = listProperty;
}
public  Set getSetProperty() {
return   setProperty ;
}
public   void  setSetProperty(Set setProperty) {
this . setProperty  = setProperty;
}
public  Map getMapProperty() {
return   mapProperty ;
}
public   void  setMapProperty(Map mapProperty) {
this . mapProperty  = mapProperty;
}
public  Properties getProperty() {
return   property ;
}
public   void  setProperty(Properties property) {
this . property  = property;
}
public   void  printInfo(){
System. out .println( "listProperty" );
System. out .println( listProperty );
System. out .println( "setProperty" );
System. out .println( setProperty );
Set set =  mapProperty .entrySet();
Iterator it = set.iterator();
while (it.hasNext()){
 Map.Entry entry = (Entry) it.next();
 System. out .println( "Key "  +entry.getKey() );
 System. out .println( "value " +entry.getValue());
}
System. out .println( "props: " );
Set set2 =  property .entrySet();
Iterator it2 = set2.iterator();
while (it2.hasNext()){
Map.Entry entry= (Entry) it2.next();
System. out .println( "key " +entry.getKey());
System. out .println( "value " +entry.getValue());
}
}
}
applcationContext.xml的写法:
< bean  id = "someBean"  class = "ioc3.SomeBean" >
< property  name = "listProperty" >
  < list >
     < value > list1 </ value >
     < value > list1 </ value >
     < value > list3 </ value >
  </ list >
</ property >
< property  name = "setProperty" >
  < set >
     < value > set1 </ value >
     < value > set1 </ value >
     < value > set3 </ value >
  </ set >
</ property >
     < property  name = "mapProperty" >
  < map >
     < entry  key = "key1" >
           < value > value1 </ value >
     </ entry >
    < entry  key = "key2" >
           < value > value2 </ value >
     </ entry >
  </ map >
</ property >
     < property  name = "property" >
      < props >
   < prop  key = "key1" > prop1 </ prop >
   < prop  key = "key2" > prop2 </ prop >
   < prop  key = "key3" > prop3 </ prop >
   </ props >
</ property >
</ bean >
测试类:Test
public   static   void  main(String[] args) {
//  TODO  Auto-generated method stub
ApplicationContext    ac =
  new  ClassPathXmlApplicationContext( "ioc3\\applicationContext.xml" );
    SomeBean sb = (SomeBean) ac.getBean( "someBean" );
    sb.printInfo();
}
(2) 、基于构造器(在A中注入B类的构造器)
在Bean中不用写set方法,但是要有构造器
例子:
public   class  SomeBean {
//构造器配置
private  String  str1 ;
private  String  str2 ;
private   int   value1 ;
public  SomeBean(String str1, String str2,  int  value1) {
super ();
this . str1  = str1;
this . str2  = str2;
this . value1  = value1;
}
public   void  printInfo(){
   System. out .println( "str1 " + str1  + "str2 " + str2 + " value1 " + value1  );
}
}
配置文件applicationContext.xml的写法
< bean  id = "someBean"  class = "ioc4.SomeBean" >
< constructor-arg >
   < value > String1 </ value >
</ constructor-arg >
< constructor-arg >
   < value > String2 </ value >
</ constructor-arg >
< constructor-arg >
   < value > 100 </ value >
</ constructor-arg >
</bean>

你可能感兴趣的:(spring,职场,笔记,休闲)