Spring 2.0(二) 深入IoC容器
在Spring中,那些组成应用的主体(backbone)及由Spring IoC容器所管理的对象被称之为bean。
bean定义以及bean相互间的依赖关系将通过配置元数据来描述。
BeanFactory是IoC容器的核心接口。它的职责包括:实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。
注入分为构造器注入和setter方式注入
1.配置元数据,配置bean之间的依赖关系。
注意:当协作bean被注入到依赖bean时,协作bean必须在依赖bean之前public ClassPathXmlApplicationContext(String configLocation) throws BeansException {this(new String[] {configLocation});}
?
BeanFactory bf = new ClassPathXmlApplicationContext(new String[]{"applicationContext1.xml","applicationContext2.xml"});??? 如果定义多个applicationContext.xml文件,并且规范了命名都以"applicationContext-"为前缀,可以用通配符:
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext-*.xml");??? 这样容器会把全部的applicationContext-开头的文件全读进来。
?
将XML配置文件分拆成多个部分是非常有用的。为了加载多个XML文件生成一个ApplicationContext实例,可以将文件路径作为字符串数组传给ApplicationContext构造器。
3.Spring IoC容器将管理一个或多个bean,这些bean将通过配置文件中的bean定义被创建。bean之间的关系,即协作 (或者称依赖)。
?