Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
1.2.1. Configuration Metadata (配置元数据)
1.2.2. Instantiating a Container(实例化一个容器)
1.2.2.1 Composing XML-based Configuration Metadata(组合基于XML的配置元数据)
1.2.2.2 The Groovy Bean Definition DSL (Groovy Bean定义DSL)
1.2.3. Using the Container (使用容器)
下载此文档精编完整版
No. | 内容 | 下载地址 | 文档内容目录 |
1 | 中英双语精编版 第一部分 | PDF下载 | 内容目录 |
2 | 中英双语精编版 第二部分 | PDF下载 | 内容目录 |
3 | 中文精编版 第一部分 | PDF下载 | 内容目录 |
4 | 中文精编版 第二部分 | PDF下载 | 内容目录 |
更多章节内容,请点击查看: Core Technologies
1.2.2. 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 构造函数的位置路径是资源字符串(resource string),它允许容器从各种外部资源(如本地文件系统、Java 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容器之后,您可能想进一步了解Spring的Resource 抽象(如Resources中所述),它提供了一种方便的机制,以从URI语法(URI syntax)定义的位置读取InputStream。特别的是,资源(Resource )路径用于构建应用程序上下文,如应用程序上下文和资源路径中( Application Contexts and Resource Paths)所述。 |
The following example shows the service layer objects (services.xml) configuration file:
以下示例展示服务层对象(service layer object)(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:
以下示例展示了数据访问对象(data access object)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对象关系映射标准(paAccountDao and JpaItemDao (based on the))的数据访问对象组成。Property name元素引用JavaBean属性的名称,ref 元素引用另一个bean定义的名称。id和ref元素之间的链接表示协作对象之间的依赖关系。有关对象依赖配置的详细信息,请参见Dependencies。