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
构造函数的位置路径实际上是允许容器从各种外部资源(如本地文件系统,从JavaCLASSPATH
等)加载配置元数据的资源字符串。
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
在你学习Spring的IoC容器之后,你可能想更多地了解Spring的Resource资源抽象,如第4章,资源*,它提供了从URI语法中定义的位置读取InputStream的方便机制。 特别是,Resource 路径用于构造应用程序上下文,如[第4.7节“应用程序上下文和资源路径”](http://docs.spring.io/spring/docs/5.0.0.M4/spring-framework-reference /htmlsingle/#resources-app-ctx) |
以下示例显示服务层对象(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>
以下示例显示数据访问对象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
类型的两个数据访问对象(基于JPA对象/关系映射标准)组成。 property name
元素是指JavaBean属性的名称,ref
元素是指另一个bean定义的名称。 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.
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:
使bean定义跨越多个XML文件可能很有用。 通常每个单独的XML配置文件表示您的架构中的逻辑层或模块。
您可以使用应用程序上下文构造函数从所有这些XML片段加载bean定义。 这个构造函数需要多个Resource
位置,如上一节所示。 或者,使用一个或多个出现的``元素从其他文件加载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, whilemessageSource.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
和themeSource.xml
。 所有位置路径都与执行导入的定义文件相关,因此services.xml
必须与执行导入的文件位于相同的目录或类路径位置,而messageSource.xml
和themeSource.xml
必须在 位于导入文件位置下方的resources
位置。 如你所见,前导斜杠被忽略,但是鉴于这些路径是相对的,所以最好不要使用斜杠。 被导入的文件的内容,包括顶层``元素,必须是根据Spring Schema的有效XML bean定义。