reference:
core:
Hello Version 5.3.1:
The org.springframework.context.ApplicationContext
interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
org.springframework.context.ApplicationContext
接口代表Spring IoC容器,并负责实例化,配置和组装Bean。
The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.
容器通过读取配置元数据,获取有关要实例化、配置和组装哪些对象的指令。
The configuration metadata is represented in XML, Java annotations, or Java code.
配置元数据以XML,Java注解或Java代码表示。
Configuration Metadata
For information about using other forms of metadata with the Spring container:
有关使用Spring容器中其他形式的元数据的信息:
the basic structure of XML-based configuration metadata:
基于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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
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
构造器的(本地一个或多个)路径是资源字符串,它可让容器从各种外部资源加载配置元数据,例如本地文件系统,JavaCLASSPATH
等。
java实现加载配置的元数据:
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
the service layer objects (services.xml)
configuration file:
服务层对象
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 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>
Composing XML-based Configuration Metadata
组成基于XML配置的元数据
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.
使bean定义跨越多个XML文件可能很有用。通常,每个单独的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:
你可以使用application context的构造函数,从这些XML片段中加载bean的定义。如上[一节中]所示,该构造函数具有多个资源位置 。或者使用一个或多个
<import/>
元素从另一个文件中加载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>
Using the Container
The ApplicationContext
lets you read bean definitions and access them,
ApplicationContext
使你能够查看bean的定义并访问它们。
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
原文:https://www.cnblogs.com/2020-6-12/p/14057077.html