特别说明
这是一个由simviso团队所组织进行的基于Spring Framework 5.2.2版本基础文档翻译。如果想要深入讨论,可扫描下方二维码,加入官方群和知秋的知识星球,免费给大家分享相关知识。
由于专业文档翻译难度比较大,我们内部本着翻译质量,也有一系列的规范,也因这些规范,消耗的时间更多,同时我们自己时间也是有限的,作为公益组织,当下也没什么收益,都是小伙伴每天晚上熬夜在做事情,请勿催更,望理解。
本节参与人员
知秋-掘金博客
https://juejin.im/user/59c764...
实例化一个容器(Instantiating a Container)
The location path or paths supplied to an ApplicationContext
constructor are resource strings that let the container load configuration metadata from a variety of external resources, such as the local file system, the Java CLASSPATH
, and so on.
将本地一个或多个资源路径字符串提供给 ApplicationContext
构造器,可使 container 从外部资源中加载配置需要的元数据。资源文件路径可以为本地文件、CLASSPATH
等。
Java
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
Kotlin
val context = ClassPathXmlApplicationContext("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 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 Application Contexts and Resource Paths.
在学习了Spring IoC container容器的相关知识后,你可能想了解更多有关Spring 中 Resource
抽象的知识 (详见 Resources ),该抽象提供了一个从URI语法定义的位置中读取InputStream的便捷机制。特别是,如 Application Contexts and Resource Paths 中所述,Resource
路径用于构造应用程序上下文。
The following example shows the service layer objects (services.xml)
configuration file:
下面的例子是一个展示了 service
层对象的配置文件 (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
https://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
https://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 PetStoreServiceImpl
class and two data access objects of the types 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
的数据访问对象组成(基于JPA对象关系映射标准)。 property name
元素引用JavaBean中属性的name,而 ref
元素引用另一个bean定义的名称(即id名称)。 id
和 ref
元素之间的这种联系表达了协作对象之间的依赖性。有关配置一个对象的依赖项的详细信息,请参见 Dependencies 。
组合基于XML的配置元数据(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文件中定义Bean是很有用的,往往,每个单独的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 <import/>
element to load bean definitions from another file or files. The following example shows how to do so:
你可以使用应用程序上下文构造函数从所有这些XML片段中加载bean定义。该构造函数可以接收多个 Resource
位置,如 上一节所示。或者,使用一个或多个 <import/>
元素来从另一个或多个xml文件中加载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. However, 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 <beans/>
element, must be valid XML bean definitions, according to the Spring Schema.
在上面的示例中,从 services.xml
, messageSource.xml
和 themeSource.xml
这三个文件来加载外部bean定义。导入的bean定义文件使用的都是本地相对路径。所以 services.xml
与执行导入的文件必须在同一个文件目录或同一个classpath 位置。而 messageSource.xml
和 themeSource.xml
必须在一个叫 resources
的位置下。如你所见,/
被忽略,鉴于 这些路径是相对的,最好不要使用 /
。关于这些文件被导入的内容,包括顶级的 <beans/>
元素,根据Spring Schema,必须是有效的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:../services.xml
)中使用。在这些URL中,程序在运行解析过程会选择“ 最近
”classpath 根路径,然后查看其父级目录。classpath配置的更改可能导致选择其他错误的目录。
你可以一直使用绝对资源路径来代替相对路径:例如,file:C:/config/services.xml
或 classpath:/config/services.xml
。但是,请注意,你会将应用程序的配置耦合到特定的绝对路径位置。通常最好为这样的绝对路径保留一个间接寻址—例如,通过设定一个针对JVM系统属性的 "${…}" 占位符,在运行时对它进行解析。
The namespace itself provides the import directive feature. Further configuration features beyond plain bean definitions are available in a selection of XML namespaces provided by Spring — for example, the context
and util
namespaces.
命名空间自身提供了import指令功能。在Spring提供的一系列XML命名空间中除了提供了除普通bean的定义,它还提供了其他配置功能。例如,context
和 util
这两种命名空间。
The Groovy Bean Definition 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 live in a ".groovy" file with the structure shown in the following example:
作为外部化配置元数据的另一个示例,Bean定义也可以在Spring的Groovy Bean定义DSL中表达,这从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定义文件。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。