<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
4.2 Container View(容器概述)
org.springframework.context.ApplicationContext接口代表Spring的IoC容器并且负责上面提及beans的实例化,配置和装配。容器通过读取配置元数据获取要实例化,配置和装配的对象的指令。可以通过XML,注解或者Java代码表示配置元数据。其允许你表达这样的对象来构成应用程序和这样对象之间的相互依赖。
ApplicationContext接口的几个实现由Spring提供开箱即用。在独立的应用程序中,通常是创建一个ClassPathXmlApplicationContext
或者ClassPathXmlApplicationContext
的实例。其中XML是定义配置元数据的传统格式,你可以命令容器使用Java注解或者代码作为元数据格式,通过提供少量的XML配置来申明支持额外的元数据格式。
在大多数应用程序场景中,显示的用户代码不要求实例化一个或者多个Spring IoC容器。例如,在一个web应用程序场景中,应用程序的web.xml文件中仅仅8行样板化的web描述符就能满足要求。如果使用SpringSource Tool Suite工具,容器生成样板化的配置。
下面的图表是Spring如何工作的高级视图。你的应用程序类通过配置元数据联合,这样在创建和初始化ApplicationContext之后,你有一个全配置和可执行的系统或者应用程序。
图 4-1 The Spring IoC Container
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
<bean/>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
Composing XML-based configuration metadata(基于XML配置的元数据)
定义跨越多个xml文件是很用的。一般每个独立的xml配置文件在你的结构中表示一个逻辑层或者模块。
你可以使用application Context构造器从xml片段中加载定义的beans。这个构造器需要多个Resource位置,如同前面显示的那样。作为可选地,使用一个或者多个<import/>元素从其他文件中加载定义的beans。例如:
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
messageSource.xml
, 和
themeSource.xml。所有的位置路径与导入的定义文件是相对的,这样service.xml必须与importing的文件在同一个目录或者类位置下,messageSource.xml
, 和themeSource.xml必须位于resources所在的目录下。如你所见,忽略了斜线,所给的路径都是相对的,不用斜线是一中较好的形式。文件内容就被导入了,包括顶层元素<beans/>必须通过Spring语法校验。
注意:
4.2.3 Using the container(使用容器)
ApplicationContext是一个高级工厂的接口,能够维护注册不同beans和其依赖。使用方法T getBean(String name, Class<T> requiredType) 你可以重新获得beans的实例。
ApplicationContext能使你读取bean和访问它们,如下所示:
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
使用getBean()重新获得beans的实例。ApplicationContext接口还有其他的方法重新获取这些beans,但是理想情况下,你的应用程序代码应该从不会使用它们。
确实,你的应用程序代码应该从不调用getBean()方法,这样就与Spring的API没有依赖。例如,Spring与web框架的集成,对不同的web框架类提供了依赖注入,比如控制器和JSF-管理的bean。