Java Spring 多个 ApplicationContext [英] Java Spring multiple ApplicationContext

查看:27
本文介绍了Java Spring 多个 ApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

根据 Spring API,ApplicationContext 是:

According the Spring API, ApplicationContext is:

  • 为应用程序提供配置的中央界面.在应用程序运行时这是只读的,但在以下情况下可能会重新加载实现支持这一点.

  • 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.

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

The root interface for accessing a Spring bean container. This is the basic client view of a bean container.

从上面,我的问题是:

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

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?

2) 如果我在一个 Java 应用程序中实例化两个 ApplicationContext(都在 main 主体中),这两个接口是否指向一个中央容器?还是两个独立的实例?看下面的代码,context1context2有什么区别?如果Beans.xml中有一个Singleton,由context1context2调用,是两个分离的实例还是同一个实例?

2) If i instantiate two ApplicationContext in one java application (both in main body), are these two interfaces to one central container? Or two separate instances? 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 instances or the same instance?

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

推荐答案

容器指的是核心 spring 控制容器反转.通过使用 reflection,并管理 Java 对象的生命周期,这些对象称为 beans托管对象.

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 beans or managed objects.

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

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).

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

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.

ApplicationContext(作为 interface 和直接实现风格)是实现这个 IoC 容器的意思,而不是 BeanFactory,它现在(一个很少使用的)<一种管理 bean 的 href="https://stackoverflow.com/questions/243385/new-to-spring-beanfactory-vs-applicationcontext?rq=1">更直接的方式,顺便提供了ApplicationContext 的基本实现功能.

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

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

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.

这篇关于Java Spring 多个 ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆