Instantiating a container

来源:互联网 发布:linux shell编程pdf 编辑:程序博客网 时间:2024/05/19 16:49

3.2.2 Instantiating a container

Instantiating a Spring IoC container is straightforward. The location path or paths supplied to an ApplicationContext constructor are actually resource strings that allow the container to load configuration metadata from a variety of external resources such as the local file system, from the Java CLASSPATH, and so on.


实例化一个Ioc容器是 简单的,ApplicationContext构造器的的参数中的资源路径,容器可以在本地文件系统或java classpath或其他中定位配置元数据。

ApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

After you learn about Spring’s IoC container, you may want to know more about Spring’s Resource abstraction, as described in Chapter 4, Resources, which provides a convenient mechanism for reading an InputStream from locations defined in a URI syntax. In particular, Resource paths are used to construct applications contexts as described in Section 4.7, “Application contexts and Resource paths”.

了解了Spring IoC容器后,你可能需要了解更过关于Spring esource 抽象那内容,第四章会详细讲解相关内容,提供了一个 根据URI语句中定义位置读取为InputStream 的方便机制。另外,第4.7 章节”应用上下文以及资源路径“中会讲解 使用Resource 构建应用程序上下文。

The following example shows the service layer objects (services.xml) configuration file:


下面的例子展示了 服务层 对象配置文件services.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>

The following example shows the data access objects daos.xml file:
下面的这个例子展示了数据接入层的配置文件 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">    <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>

In the preceding example, the service layer consists of the class PetStoreServiceImpl, and two data access objects of the type JpaAccountDao and JpaItemDao (based on the JPA Object/Relational mapping standard). The property name element refers to the name of the JavaBean property, and the ref element refers to the name of another bean definition. This linkage between id and ref elements expresses the dependency between collaborating objects. For details of configuring an object’s dependencies, see Dependencies.


前面的例子中,服务层包括PetStoreServiceImpl 类以及 两个数据访问对象 JpaAccountDao 和JpaItemDao(基于JAP 对象/关系映射) property 的name 节点指定该javabean的名称 ref指向另一个bean ,id 和ref 节点的关系,展示了对象之间的依赖,有关 对象依赖关系的详细信息 查看 依赖关系。

Composing XML-based configuration metadata

编写 基础XML 的配置元数据

It can be useful to have bean definitions span multiple XML files. Often each individual XML configuration file represents a logical layer or module in your architecture.


有必要将 bean 定义 分成多个XML 文件,每个单独的XML 配置文件 代表了你的架构中一个 逻辑层或者 模块。

You can use the application context constructor to load bean definitions from all these XML fragments. This constructor takes multiple Resource locations, as was shown in the previous section. Alternatively, use one or more occurrences of the element to load bean definitions from another file or files. For example:


你可以应用程序的上下文构造器从所有的xml片段中读取bean的定义。构造器支持多个资源的路径就像前面的那个例子。或者,使用一个或者多个标签来从其他文件中加载bean定义。例如:

<beans>    <import resource="services.xml"/>    <import resource="resources/messageSource.xml"/>    <import resource="/resources/themeSource.xml"/>    <bean id="bean1" class="..."/>    <bean id="bean2" class="..."/></beans>

In the preceding example, external bean definitions are loaded from three files: services.xml, messageSource.xml, and themeSource.xml. All location paths are relative to the definition file doing the importing, so services.xml must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. As you can see, a leading slash is ignored, but given that these paths are relative, it is better form not to use the slash at all. The contents of the files being imported, including the top level element, must be valid XML bean definitions according to the Spring Schema.


上面的例子中,从下面三个文件中加载扩展bean定义ervices.xml, messageSource.xml,以及themeSource.xml。所有文件路径都是导入bean定义文件路径是由管的,所有services.xml必须和正在导入beans的文件在同一个目录或者同一个类路径下。 messageSource.xml 和themeSource.xm 在正在导入beans的文件目录所在的下一级目录resources 。正如你看到的
第一个斜杠被忽略的,但是鉴于这个路径是相对的,所以尽量不要使用斜杠。导入bean的文件正文中包括了一个顶层节点<beans> 必须是一个遵循 Spring 规范的 有效XML bean 定义

It is possible, but not recommended, to reference files in parent directories using a relative “../” path. Doing so creates a dependency on a file that is outside the current application. In particular, this reference is not recommended for “classpath:” URLs (for example, “classpath:../services.xml”), where the runtime resolution process chooses the “nearest” classpath root and then looks into its parent directory. Classpath configuration changes may lead to the choice of a different, incorrect directory.
You can always use fully qualified resource locations instead of relative paths: for example, “file:C:/config/services.xml” or “classpath:/config/services.xml”. However, be aware that you are coupling your application’s configuration to specific absolute locations. It is generally preferable to keep an indirection for such absolute locations, for example, through “${…​}” placeholders that are resolved against JVM system properties at runtime.


使用”../”去引用父目录中的文件是可行的但是不推荐,这样做会产生对程序以外的文件的依赖,另外也不推荐在classpath:"URLs"中使用 (例如”classpath:../service.xml“), 在运行的时候,解析进程会选择最近的classpath ,然后去寻找他的父目录。 classpath 配置发生变化。导致 解析集成 选择目录失败,你也可以使用 resource 全限定路径 来替代相对路径,例如”file:C:/config/services.xml“,”classpath:/config/services.xml“。但是,你需要明确你的应用程序配置文件和路径产生了耦合关系,通常下更好的方式是在这些觉得路径间保持一个间接的关系,例如在运行时使用 “${…​}” 占位符结局JVM属性。


The import directive is a feature provided by the beans namespace itself. Further configuration features beyond plain bean definitions are available in a selection of XML namespaces provided by Spring, e.g. the “context” and the “util” namespace.


impact 指令时 beans 命名空间本身提供的一个功能,Spring提供了一些命名空间,如”context“,”util“,可以使用 beans 本身命名空间的一些扩展功能。

The Groovy Bean Definition DSL

Groovy Bean 配置 Dsl

As a further example for externalized configuration metadata, bean definitions can also be expressed in Spring’s Groovy Bean Definition DSL, as known from the Grails framework. Typically, such configuration will live in a “.groovy” file with a structure as follows:


作为使用外部配置元数据文件的另一个例子,也可以使用Spring的Groovy Bean Definition DSL 来标识bean,如Grails框架说述,通常这个配置会存在一个结构如下文的”.groovy“文件 中。

beans {    dataSource(BasicDataSource) {        driverClassName = "org.hsqldb.jdbcDriver"        url = "jdbc:hsqldb:mem:grailsDB"        username = "sa"        password = ""        settings = [mynew:"setting"]    }    sessionFactory(SessionFactory) {        dataSource = dataSource    }    myService(MyService) {        nestedBean = { AnotherBean bean ->            dataSource = dataSource        }    }}

This configuration style is largely equivalent to XML bean definitions and even supports Spring’s XML configuration namespaces. It also allows for importing XML bean definition files through an “importBeans” directive.


这种配置方式很大程度上与XML bean 定义类似,甚至支持Spring XML 配置命名空间,他还允许使用 ”importBeans“指令来导入 XML bean定义文件。

原创粉丝点击