Java Spring 多个 ApplicationContext
Java Spring multiple ApplicationContext
springApplicationContext
的定义很模糊,我都快看完一整本书的教程了,还是搞不懂ApplicationContext
是什么意思
根据SpringAPI,ApplicationContext
是:
Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if
the implementation supports this.
The root interface for accessing a Spring bean container. This is the basic client view of a bean container.
综上所述,我的问题是:
1) 一直看到书上提到"container",容器指的是什么?一个容器是否意味着一个 java 进程?或者一个容器引用一个 ApplicationContext
对象?
2) 如果我在一个 java 应用程序中实例化两个 ApplicationContext
(都在 main
主体中),这两个接口是否指向一个中央容器?还是两个不同的实例?看下面的代码,context1
和context2
有什么区别?如果Beans.xml
中有一个Singleton,它被context1
和context2
调用,它们是两个独立的实例还是同一个实例?
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");
您添加了一个 "java-ee" 标签。 Spring 通常用于应用程序服务器上的 Web 应用程序 运行。通常每个 Web 应用程序都有自己的应用程序。 Web 应用程序是分开的,可能这就是文档中所谓的容器,因为您不能定期与不同的应用程序/容器共享变量。
一个应用程序中可以有两个上下文。如果您有两个上下文,每个上下文都会有自己的单例。
容器是一个 Java 对象,是 ApplicationContext
实现之一的实例,如 ClassPathXmlApplicationContext
。
是2个不同的容器,如果Beans.xml
有一个singleton bean B1,那么context1.getBean("B1")
和context2.getBean("B1")
就会return2个不同的实例B1
通过容器,它们引用核心 spring Inversion of Control container. The container provides a way to initialize/bootstrap your application (loading the configuration in xml files or annotations), through use of reflection,并管理 Java 个对象的生命周期,这些对象称为 beans或 托管对象 。
在此初始阶段,您没有(通常,但有可能)控制您的应用程序,相反,您将在引导完成时获得应用程序的完全初始化状态(或什么也没有,以防万一)失败)。
它是所谓的 EJB3 容器 的替代品,也可能是补充品;然而,spring 所提供的未能遵守 EJB 定义的标准。从历史上看,EJB 的采用受到该规范的复杂性的限制,spring 是一个新创建的项目,用于在 J2SE jvm 上具有 EJB3 可比较的功能 运行 而没有 EJB 容器,并且更容易配置。
ApplicationContext
(作为管理 beans 的 interface, and by the direct implementation flavours) is the mean of implementing this IoC container, as opposed to the BeanFactory
, which is now (a sparsely used and) more direct way,顺便说一句,它为 ApplicationContext 提供了基本实现功能。
根据你的第二个问题,你可以有多个 ApplicationContexts 实例,在这种情况下,它们将完全隔离,每个实例都有自己的配置。
I keep seeing the book mentioned "container", what is the container
refer to? One container does it mean one java process? or one
container refer to one ApplicationContext object?
这里的container是指spring容器,也就是ApplicationContext。这会在内部读取 spring 配置并根据配置加载 类。您可能认为它是 SpringProcessor,它提供各种现成的功能,如 bean 初始化、注入、i18n、bean Post 处理等
with
ApplicationContext context1 = new
ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext
context2 = new ClassPathXmlApplicationContext("Beans.xml");
将有两个容器,因此有两个单例 bean。这里单例意味着每个容器的单例实例。理想情况下,您应该只有一个容器,除非您有两个容器。出于学习目的,理解这些概念是有意义的
ApplicationContext 是 Spring 容器的实现。简单来说,Spring 容器通过 ApplicationContext 管理整个 Spring 应用程序。 Spring 容器通过 ApplicationContext 管理 Spring bean 的生命周期,即从启动到销毁。
spring 容器嵌套在 J2EE 容器中。
I keep seeing the book mentioned "container", what is the container
refer to? One container does it mean one java process? or one
container refer to one ApplicationContext object?
容器管理对象的生命周期。 Tomcat 是一个容器的例子。就像 Spring 容器通过 ApplicationContext 管理应用程序一样,J2EE 容器 Tomcat 通过 web.xml
管理应用程序
容器提供通信支持。 Web 应用程序中的安全性。 JSP 支持、国际化、事件传播和许多其他功能。它支持多线程,为每个资源请求生成一个新线程。您不必为此明确编写代码。就像 spring 容器一样,J2ee 容器管理一个 servlet 生命周期。
If i instantiate two ApplicationContext in one java application (one
Main body), are these two interface to one central container?
如果你想在你的应用程序中实例化多个ApplicationContext。它将位于父子层次结构中。将有一个根 ApplicationContext & 然后将有对应于每个 DispatcherServlet 的子 ApplicationContext。应用程序的全局 Bean 将在根 ApplicationContext 中定义。所有 ApplicationContext 将仅由一个 spring 容器管理。
首先你的问题:
1) I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?
ApplicationContext 是中央接口,但底层容器是 BeanFactory
。更确切地说,BeanFactory
是一个较低级别的接口,由您从中获取 Bean 的所有应用程序上下文实现。从这个意义上说,我认为 container 这个词在这里代表 BeanFactory
- 每个 ApplicationContext 一个。
2) If i instantiate two ApplicationContext in one java application (one Main body), are these two interface to one central container? Or two separate different instance? See the code below, what is the difference between context1 and context2? If there is a Singleton in Beans.xml, it is invoked by context1 and context2, are they two separated instance or same instance?
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");>
通过这些实例化,您将获得 2 个完全独立的应用程序上下文。在第一个中声明的一个 bean 将不会在另一个中找到。
但是
在一个 Web 应用程序中有多个应用程序上下文是很常见的,因为 Spring 有一个 ApplicationContext 层次结构的概念。您可以将它们声明为:
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml", context1);>
在这里您只能从 context1
中检索其中声明的 bean,但是从 context2
中您将从 context2
和 context1
。具体来说,bean 首先在 context2
中查找,如果找不到则在 context1
.
中查找
这在 Spring MVC 中使用,您通常有一个根上下文(对于所有与 MVC DispatcherServlet
不直接相关的 bean)和一个专用于 DispatcherServlet
的子上下文它将包含用于控制器、视图、拦截器等的 bean。
我一直看到书上提到“容器”,容器指的是什么?一个容器是否意味着一个 java 进程?或者一个容器引用一个ApplicationContext对象?
在 Spring 中,构成应用程序 backbone 并由 Spring IoC 容器管理的对象称为 bean。 bean 是由 Spring IoC 容器实例化、组装和管理的对象。
接口org.springframework.context.ApplicationContext表示Spring IoC容器。容器获取详细信息或说明,即需要通过读取配置元数据来实例化、配置和组装哪些对象。
应用程序 类 与配置元数据相结合,因此在创建和初始化 ApplicationContext 后,您将拥有一个完全配置且可执行的系统或应用程序。
- 如果我在一个 java 应用程序(一个主体)中实例化两个 ApplicationContext,这两个接口是否指向一个中央容器?还是两个独立的不同实例?看下面的代码,context1和context2有什么区别?如果Beans.xml中有一个Singleton,它被context1和context2调用,它们是两个独立的实例还是同一个实例?
正如其他人所提到的,您也可以有多个应用程序上下文。
springApplicationContext
的定义很模糊,我都快看完一整本书的教程了,还是搞不懂ApplicationContext
是什么意思
根据SpringAPI,ApplicationContext
是:
Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
The root interface for accessing a Spring bean container. This is the basic client view of a bean container.
综上所述,我的问题是:
1) 一直看到书上提到"container",容器指的是什么?一个容器是否意味着一个 java 进程?或者一个容器引用一个 ApplicationContext
对象?
2) 如果我在一个 java 应用程序中实例化两个 ApplicationContext
(都在 main
主体中),这两个接口是否指向一个中央容器?还是两个不同的实例?看下面的代码,context1
和context2
有什么区别?如果Beans.xml
中有一个Singleton,它被context1
和context2
调用,它们是两个独立的实例还是同一个实例?
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");
您添加了一个 "java-ee" 标签。 Spring 通常用于应用程序服务器上的 Web 应用程序 运行。通常每个 Web 应用程序都有自己的应用程序。 Web 应用程序是分开的,可能这就是文档中所谓的容器,因为您不能定期与不同的应用程序/容器共享变量。
一个应用程序中可以有两个上下文。如果您有两个上下文,每个上下文都会有自己的单例。
容器是一个 Java 对象,是
ApplicationContext
实现之一的实例,如ClassPathXmlApplicationContext
。是2个不同的容器,如果
Beans.xml
有一个singleton bean B1,那么context1.getBean("B1")
和context2.getBean("B1")
就会return2个不同的实例B1
通过容器,它们引用核心 spring Inversion of Control container. The container provides a way to initialize/bootstrap your application (loading the configuration in xml files or annotations), through use of reflection,并管理 Java 个对象的生命周期,这些对象称为 beans或 托管对象 。
在此初始阶段,您没有(通常,但有可能)控制您的应用程序,相反,您将在引导完成时获得应用程序的完全初始化状态(或什么也没有,以防万一)失败)。
它是所谓的 EJB3 容器 的替代品,也可能是补充品;然而,spring 所提供的未能遵守 EJB 定义的标准。从历史上看,EJB 的采用受到该规范的复杂性的限制,spring 是一个新创建的项目,用于在 J2SE jvm 上具有 EJB3 可比较的功能 运行 而没有 EJB 容器,并且更容易配置。
ApplicationContext
(作为管理 beans 的 interface, and by the direct implementation flavours) is the mean of implementing this IoC container, as opposed to the BeanFactory
, which is now (a sparsely used and) more direct way,顺便说一句,它为 ApplicationContext 提供了基本实现功能。
根据你的第二个问题,你可以有多个 ApplicationContexts 实例,在这种情况下,它们将完全隔离,每个实例都有自己的配置。
I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?
这里的container是指spring容器,也就是ApplicationContext。这会在内部读取 spring 配置并根据配置加载 类。您可能认为它是 SpringProcessor,它提供各种现成的功能,如 bean 初始化、注入、i18n、bean Post 处理等
with
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");
将有两个容器,因此有两个单例 bean。这里单例意味着每个容器的单例实例。理想情况下,您应该只有一个容器,除非您有两个容器。出于学习目的,理解这些概念是有意义的
ApplicationContext 是 Spring 容器的实现。简单来说,Spring 容器通过 ApplicationContext 管理整个 Spring 应用程序。 Spring 容器通过 ApplicationContext 管理 Spring bean 的生命周期,即从启动到销毁。
spring 容器嵌套在 J2EE 容器中。
I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?
容器管理对象的生命周期。 Tomcat 是一个容器的例子。就像 Spring 容器通过 ApplicationContext 管理应用程序一样,J2EE 容器 Tomcat 通过 web.xml
管理应用程序容器提供通信支持。 Web 应用程序中的安全性。 JSP 支持、国际化、事件传播和许多其他功能。它支持多线程,为每个资源请求生成一个新线程。您不必为此明确编写代码。就像 spring 容器一样,J2ee 容器管理一个 servlet 生命周期。
If i instantiate two ApplicationContext in one java application (one Main body), are these two interface to one central container?
如果你想在你的应用程序中实例化多个ApplicationContext。它将位于父子层次结构中。将有一个根 ApplicationContext & 然后将有对应于每个 DispatcherServlet 的子 ApplicationContext。应用程序的全局 Bean 将在根 ApplicationContext 中定义。所有 ApplicationContext 将仅由一个 spring 容器管理。
首先你的问题:
1) I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?
ApplicationContext 是中央接口,但底层容器是 BeanFactory
。更确切地说,BeanFactory
是一个较低级别的接口,由您从中获取 Bean 的所有应用程序上下文实现。从这个意义上说,我认为 container 这个词在这里代表 BeanFactory
- 每个 ApplicationContext 一个。
2) If i instantiate two ApplicationContext in one java application (one Main body), are these two interface to one central container? Or two separate different instance? See the code below, what is the difference between context1 and context2? If there is a Singleton in Beans.xml, it is invoked by context1 and context2, are they two separated instance or same instance?
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");>
通过这些实例化,您将获得 2 个完全独立的应用程序上下文。在第一个中声明的一个 bean 将不会在另一个中找到。
但是
在一个 Web 应用程序中有多个应用程序上下文是很常见的,因为 Spring 有一个 ApplicationContext 层次结构的概念。您可以将它们声明为:
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml", context1);>
在这里您只能从 context1
中检索其中声明的 bean,但是从 context2
中您将从 context2
和 context1
。具体来说,bean 首先在 context2
中查找,如果找不到则在 context1
.
这在 Spring MVC 中使用,您通常有一个根上下文(对于所有与 MVC DispatcherServlet
不直接相关的 bean)和一个专用于 DispatcherServlet
的子上下文它将包含用于控制器、视图、拦截器等的 bean。
我一直看到书上提到“容器”,容器指的是什么?一个容器是否意味着一个 java 进程?或者一个容器引用一个ApplicationContext对象?
在 Spring 中,构成应用程序 backbone 并由 Spring IoC 容器管理的对象称为 bean。 bean 是由 Spring IoC 容器实例化、组装和管理的对象。
接口org.springframework.context.ApplicationContext表示Spring IoC容器。容器获取详细信息或说明,即需要通过读取配置元数据来实例化、配置和组装哪些对象。
应用程序 类 与配置元数据相结合,因此在创建和初始化 ApplicationContext 后,您将拥有一个完全配置且可执行的系统或应用程序。
- 如果我在一个 java 应用程序(一个主体)中实例化两个 ApplicationContext,这两个接口是否指向一个中央容器?还是两个独立的不同实例?看下面的代码,context1和context2有什么区别?如果Beans.xml中有一个Singleton,它被context1和context2调用,它们是两个独立的实例还是同一个实例?
正如其他人所提到的,您也可以有多个应用程序上下文。