springframework【3】
Bean的依赖设置与集合对象的处理
?
Bean的依赖设置
?1)、type2IOC
? 最常用的通过setter方法来设置bean之间的依赖关系
?2)、type3IOC
? 通过构造函数来设置bean之间的依赖关系
?
?Autoware:自动绑定,通过设定bean的这个属性来在bean定义文件中找相应的bean来注入到bean中,他的值可以为 byName,byType,constructor,autodetect
?
?Bean对集合对象的处理
? 对于那些数组、java.util.List、java.util.Set、java.util.Map等集合对象,可以通过spring的IOC容器来自动创建维护并生成集合。常见写法如下:
例如:
? 代码类如下:
?
?
public class SomeBean {private String[] someArray;private List someList;private Some[] someObjArray;private Map someMap;public String[] getSomeArray() {return someArray;}public void setSomeArray(String[] someArray) {this.someArray = someArray;}public List getSomeList() {return someList;}public void setSomeList(List someList) {this.someList = someList;}public Some[] getSomeObjArray() {return someObjArray;}public void setSomeObjArray(Some[] someObjArray) {this.someObjArray = someObjArray;}public Map getSomeMap() {return someMap;}public void setSomeMap(Map someMap) {this.someMap = someMap;}}
?
<bean id="someBean" name="code">//将集合类Bean的属性值一一打印出来public class SpringBeanListMain {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("list.xml");SomeBean s= (SomeBean) ctx.getBean("someBean");for(String a:s.getSomeArray()){System.out.println(a);}System.out.println("------------");for(Some a:s.getObjArray()){System.out.println(a);}System.out.println("------------");for(Object a:s.getSomeList()){System.out.println(a);}System.out.println("------------");Set<java.util.Map.Entry<String, String>> it = s.getSomeMap().entrySet();for(java.util.Map.Entry<String, String> a: it){System.out.println(a.getKey());} System.out.println(s.getSomeMap().get("aaa"));}}
?