Java Spring 多个 ApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29862681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:43:02  来源:igfitidea点击:

Java Spring multiple ApplicationContext

javaspringweb-applicationsdependency-injection

提问by GMsoF

The definition of the spring ApplicationContextis very ambiguous, I almost finish a whole book of tutorial but still cannot understand what is the ApplicationContextstand for.

spring的定义ApplicationContext很模糊,我几乎读完了一整本书的教程,但仍然无法理解它的含义ApplicationContext

According the Spring API, ApplicationContextis:

根据 Spring API,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.

  • 为应用程序提供配置的中央接口。这在应用程序运行时是只读的,但如果实现支持它可能会重新加载。

  • 访问 Spring bean 容器的根接口。这是 bean 容器的基本客户端视图。

From above, my questions are:

从上面,我的问题是:

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 ApplicationContextobject?

1)我一直看到书上提到“容器”,容器指的是什么?一个容器就代表一个java进程吗?还是一个容器引用一个ApplicationContext对象?

2) If i instantiate two ApplicationContextin one java application (both in mainbody), are these two interfaces to one central container? Or two separate instances? See the code below, what is the difference between context1and context2? If there is a Singleton in Beans.xml, it is invoked by context1and context2, are they two separated instances or the same instance?

2)如果我实例化两个ApplicationContext合一的java应用程序(都在main主体中),这两个接口是一个中央容器吗?还是两个独立的实例?请参阅下面的代码是什么之间的区别context1context2?如果在 中有一个 Singleton Beans.xml,它是由context1and调用的context2,它们是两个单独的实例还是同一个实例?

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");

采纳答案by guido

By container they refer to the core 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, and to manage the lifecycle of Java objects, which are called beansor managed objects.

通过容器,它们指的是核心 spring控制反转容器。容器提供了一种方法来初始化/引导您的应用程序(在 xml 文件或注释中加载配置),通过使用反射,并管理 Java 对象(称为bean托管对象)的生命周期。

During this initial phase, you do not have (normally, yet it is possible) control in your application, instead you will get a completely initialized state for the application when the bootstrapping is done (or nothing, in case it fails).

在这个初始阶段,您没有(通常但有可能)控制您的应用程序,相反,您将在引导完成时获得应用程序的完全初始化状态(或者什么都没有,以防失败)。

It is a replacement, or possibly an addition, to what is called an EJB3 container; yet, the spring provided one fails to adhere to the EJB defined standard. Historically, adoption of EJB has been limited by the complexity of that specification, with spring being a newly created project for having EJB3 comparable features running on a J2SE jvm and without an EJB container, and with much easier configuration.

它是对所谓的EJB3 容器的替代或可能的补充;然而,提供的弹簧未能遵守 EJB 定义的标准。从历史上看,EJB 的采用受到该规范复杂性的限制,spring 是一个新创建的项目,用于在 J2SE jvm 上运行 EJB3 类似功能,并且没有 EJB 容器,并且配置更容易。

ApplicationContext(as an 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 wayof managing beans, which by the way provides the base implementation features for the ApplicationContext.

ApplicationContext(作为一个接口,并通过直接实现风格)是实现这个 IoC 容器的手段,而不是BeanFactory,它现在是(一种很少使用和)管理 bean 的更直接的方式顺便说一下,它提供了基本实现ApplicationContext 的功能。

As per your second question, you can have multiple instances of ApplicationContexts, in that case, they will be completely isolated, each with its own configuration.

根据您的第二个问题,您可以有多个 ApplicationContexts 实例,在这种情况下,它们将完全隔离,每个实例都有自己的配置。

回答by Udo Held

You added a "java-ee" tag. Spring is often used in web applications running on a application server. Typically each web application would have it's own application. The web applications are separated and probably that is what is called container in the documentation as you cannot regularly share variables with different applications / containers.

您添加了一个“java-ee”标签。Spring 常用于在应用服务器上运行的 Web 应用程序。通常,每个 Web 应用程序都有自己的应用程序。Web 应用程序是分开的,可能这就是文档中所谓的容器,因为您不能定期与不同的应用程序/容器共享变量。

You can have two contexts within an application. If you have two contexts each will have its own singleton.

一个应用程序中可以有两个上下文。如果你有两个上下文,每个上下文都有自己的单例。

回答by Evgeniy Dorofeev

  1. container is a Java object, an instance of one of ApplicationContextimplementations like ClassPathXmlApplicationContext.

  2. It is 2 different containers, if Beans.xmlhas a singleton bean B1, then context1.getBean("B1")and context2.getBean("B1")will return 2 different instances of B1

  1. 容器是一个 Java 对象,是一种ApplicationContext实现的实例,如ClassPathXmlApplicationContext.

  2. 它是 2 个不同的容器,如果Beans.xml有一个单例 bean B1,则context1.getBean("B1")andcontext2.getBean("B1")将返回 2 个不同的实例B1

回答by M Sach

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?

我一直看到书上提到“容器”,容器指的是什么?一个容器就代表一个java进程吗?还是一个容器引用一个 ApplicationContext 对象?

Here container means spring container which is nothing but ApplicationContext. This internally reads spring configuration and loads the classes based on configuration. You may think it as SpringProcessor which provides the various functionality like bean initialization, injection, i18n, bean Post processing etc off the shelf

这里的容器指的是 Spring 容器,它只不过是 ApplicationContext。这在内部读取 spring 配置并根据配置加载类。你可能认为它是 SpringProcessor,它提供了各种功能,如 bean 初始化、注入、i18n、bean 后处理等现成的

with

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");

There will be two conatiners , hence two singleton beans. Here singleton means singleton instance per container. Ideally you should have only one container until and unless you have a reason for two. For learning purpose it makes sense to understand the concepts

将有两个 conatiners ,因此有两个单例 bean。这里的单例意味着每个容器的单例实例。理想情况下,您应该只有一个容器,除非您有两个的理由。出于学习目的,理解概念是有意义的

回答by underdog

ApplicationContext is an implementation of a Spring container. In simple words Spring container manages the entire Spring application via an ApplicationContext. Spring container via ApplicationContext manages the life cycle of a Spring bean i;e from initiation to destruction.

ApplicationContext 是 Spring 容器的一个实现。简单来说,Spring 容器通过 ApplicationContext 管理整个 Spring 应用程序。Spring 容器通过 ApplicationContext 管理 Spring bean 的生命周期,即从启动到销毁。

A spring container is nested inside a J2EE container.

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?

我一直看到书上提到“容器”,容器指的是什么?一个容器就代表一个java进程吗?还是一个容器引用一个 ApplicationContext 对象?

A container manages the life cycle of an object. Tomcat is a an example of a container. Just like Spring container manages the app via ApplicationContext a J2EE container Tomcat manages the app via web.xml

容器管理对象的生命周期。Tomcat 是容器的一个示例。就像 Spring 容器通过 ApplicationContext 管理应用程序一样,J2EE 容器 Tomcat 通过 web.xml 管理应用程序

A container provides communications support. Security in a web application. JSP support, Internationalization, event-propagation & many other features. It supports multithreading, spawns a new thread for every request for a resource. You don't have to explicitly write the code for that. Just like a spring container, a J2ee container manages a servlet life cycle.

容器提供通信支持。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?

如果我在一个 Java 应用程序(一个主体)中实例化两个 ApplicationContext,这两个接口是否指向一个中央容器?

If you want to instantiate multiple ApplicationContexts in your application. It will be in a parent child hierarchy. There will be one root ApplicationContext & then there will be child ApplicationContext respective to every DispatcherServlet. Beans global to the application will be defined in the root ApplicationContext. All the ApplicationContexts will be managed by only one spring container.

如果要在应用程序中实例化多个 ApplicationContext。它将在父子层次结构中。将有一个根 ApplicationContext & 然后将有对应于每个 DispatcherServlet 的子 ApplicationContext。应用程序的全局 Bean 将在根 ApplicationContext 中定义。所有的 ApplicationContexts 将只由一个 spring 容器管理。

回答by Serge Ballesta

First you questions :

首先你的问题:

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?

1)我一直看到书上提到“容器”,容器指的是什么?一个容器就代表一个java进程吗?还是一个容器引用一个 ApplicationContext 对象?

The ApplicationContext is the central interface, but the underlying container is a BeanFactory. More exactly, BeanFactoryis a lower level interface implemented by all Application contextes from which you get the Beans. In that sense, I think that the word containerstands here for the BeanFactory- one per ApplicationContext.

ApplicationContext 是中央接口,但底层容器是BeanFactory. 更确切地说,BeanFactory是由您从中获取 Bean 的所有应用程序上下文实现的较低级别的接口。从这个意义上说,我认为容器这个词 在这里代表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)如果我在一个java应用程序(一个主体)中实例化两个ApplicationContext,这两个接口是一个中央容器吗?还是两个独立的不同实例?看下面的代码,context1和context2有什么区别?如果Beans.xml中有一个Singleton,它被context1和context2调用,它们是两个分离的实例还是同一个实例?

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");>

With that instanciations, you will get 2 totally independent application contexts. One bean declared in first will not be found in the other.

通过该实例,您将获得 2 个完全独立的应用程序上下文。在 first 中声明的一个 bean 将不会在另一个中找到。

BUT

It is common to have more than one application context in a Web application, because Spring has a notion of hierachies of ApplicationContext. You could declare them as :

在一个 Web 应用程序中拥有多个应用程序上下文是很常见的,因为 Spring 有一个 ApplicationContext 层次结构的概念。您可以将它们声明为:

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml", context1);>

Here you can retrieve from context1only beans declared in it, but from context2you will retrieve beans from context2andcontext1. Specifically, beans are first looked for in context2and if not found then looked for in context1.

在这里,您只能从其中context1声明的 bean 中检索,但从context2您将检索来自context2和 的bean context1。具体来说,首先在 中查找 bean context2,如果未找到,则在 中查找context1

This is used in Spring MVC where you normally have one root context (for all beans not directly related to the MVC DispatcherServlet) and one child context dedicated to the DispatcherServletthat will contain the beans for controllers, views, interceptors, etc.

这在 Spring MVC 中使用,您通常有一个根上下文(用于与 MVC 不直接相关的所有 bean DispatcherServlet)和一个专用于DispatcherServlet包含用于控制器、视图、拦截器等的 bean 的子上下文。