Spring 5.0.8.RELEASE文档 Core1-1.2.2 实例化容器

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.

实例化Spring IoC容器是简单直接的。只需要传入路径(多个路径即读取多个配置文件)字符串给ApplicationContext的构造函数即可,容器能从多种多样的外部。方式如文件系统、Java ClassPath等加载配置元数据。

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

(提示:你可能会想知道更多Spring资源抽象的方式,例如从URL中读取配置文件,详细你可以查看2.1和2.7章节)

The following example shows the service layer objects (services.xml) configuration file:
下面的例子是展示服务类的配置文件:

<?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:
下面的例子是展示dao类的配置文件:

<?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以及其依赖了两个dao类:JpaAccountDao ,JpaItemDao 所构成(基于JPA对象/关系映射标准)。name属性是对应set进去这个bean的属性名,而ref属性是引入其他bean的id定义,这样的id和ref的连接就能描述合作类之间的依赖。详细的依赖配置信息你可以参考1.4章节。

Composing XML-based configuration metadata
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.

构成基于xml文件的配置元数据
它能够有效地在不同的xml文件中分割多个bean的定义。通常以应用的逻辑分层来分割不同的xml配置文件(如service、dao逻辑分层分开不同的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:

你可以使用 application context 的构造函数去加载多个xml片段的bean定义。这个构造函数能接受多个资源路径。另外,也可以使用import标签去加载其他xml配置文件,例如:

<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定义文件是从 services.xml, messageSource.xml, and themeSource.xml 三个文件加载进来的,所有的路径都是相对与这个import它们的文件位置,因此上述配置看来,services.xml必须是与发起import的配置文件同一目录,而messageSource.xml 和 themeSource.xml 则是在同目录下的 resources 文件夹下。正如你所看到的,一个斜杠被忽略了,但是考虑到这些路径是相对的,最好不要使用斜线。被导入的配置文件的内容,包括beans元素,编写时必须要校验好是跟Spring所规定的格式相符才可以。

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: 这样的URL写法,例如 classpath:../services.xml,描述的是classess路径下回退上一层的 services.xml 文件,如果classPath改变会导致找不到上一层有services.xml这文件,或出现不对的路径目录。
你也可以使用完整的全路径来代替相对路径(根据首次加载的文件再import的相对位置),例如 “file:C:/config/services.xml” 或者 “classpath:/config/services.xml”。但是你会意识到你的程序配置指定了绝对路径后前缀统一部分会产生耦合(不独立,要修改时改了一边还要改另外一边)。通常是建议你定义好绝对的根路径如 c:/test ,然后通过间接的方式,通过${…}美元符号的方式来引用定义的根路径。或者没在第一个属性配置文件(.properties)定义时则会读取JVM系统属性(从在启动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.

import指令是由beans这一命名空间提供。Spring提供更多命名空间给予进行配置(多种配置特性),例如在context和util命名空间。(所以Spring要求xml的格式要符合Spirng的规范)

The Groovy Bean Definition DSL
Groovy Bean 的定义(DSL),这个是使用groovy的语法来定义bean,这部分本人暂时无用到因此不看下去了。需要的话可以去官方文档查看如何使用groovy语法来定义bean。The Groovy Bean Definition DSL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值