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

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

问题描述

弹簧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 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(作为接口,并通过直接实现实现)是实现此IoC容器的手段,与BeanFactory相对,后者现在(稀少使用)和

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天全站免登陆