這是一個由simviso團隊所組織進行的基於Spring Framework 5.2.2版本基礎文檔翻譯。若是想要深刻討論,可掃描下方二維碼,加入官方羣和知秋的知識星球,免費給你們分享相關知識。spring
因爲專業文檔翻譯難度比較大,咱們內部本着翻譯質量,也有一系列的規範,也因這些規範,消耗的時間更多,同時咱們本身時間也是有限的,做爲公益組織,當下也沒什麼收益,都是小夥伴天天晚上熬夜在作事情,請勿催更,望理解。
sql
本節參與人員express
知秋-掘金博客segmentfault
https://juejin.im/user/59c764...session
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
等。app
Java框架
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
Kotlinide
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 。
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
這兩種命名空間。
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定義文件。