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" class="com.itcast.collect.SomeBean">
<property name="someArray">
<list>
<value>aaa</value>
<value>bbb</value>
</list>
</property>
<property name="someList">
<list>
<ref bean="some1"/>
<ref bean="some2"/>
</list>
</property>
<property name="someObjArray">
<list>
<ref bean="some1"/>
</list>
</property>
<property name="someMap">
<map>
<entry key="aaaKey">
<value>aaaValue</value>
</entry>
<entry key="bbbKey">
<value>bbbValue</value>
</entry>
</map>
</property>
</bean>
以上只是说明这些集合类别的bean的声明。相当于给我们的这些bean的属性值赋予初始值了。大家可以通过代码调用,来查看是否真的被赋值了,下边是调用代码:
//将集合类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"));
}
}
分享到:
相关推荐
spring framework 3 教程
springframework3.jar
Spring Framework3教程 翻译英文资料。
SpringFramework3源码。解压后,在Eclipse或MyEclipse中建立java工程,导入即可。
spring framework 中文参考手册
org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....
Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...
Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
Spring Framework 4.x Reference Documentation 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...
Spring Framework 4.0.2.RELEASE API,该文档来自官网经本人制作而成方便使用的chm格式。
org.springframework.core.jar
This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...
org.springframework.web.jar,没问题的,最新的。
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
springframework2.5源代码