为什么要使用Spring ApplicationContext层次结构? [英] Why use Spring ApplicationContext hierarchies?

查看:274
本文介绍了为什么要使用Spring ApplicationContext层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在春季了解ApplicationContext层次结构.

I am trying to understand ApplicationContext hierarchies in spring.

我学到了以下内容

  1. ApplicationContext不能具有 超过1位家长 ApplicationContext.
  2. 当给定 ApplicationContext无法解析 豆,它将通过解析 向其父请求.
  3. ...的父母 指定了一个ApplicationContext 在其构造函数中.
  1. An ApplicationContext cannot have more than 1 parent ApplicationContext.
  2. When a given ApplicationContext cannot resolve a bean, it will pass on the resolution request to its parent.
  3. The parent of an ApplicationContext is specified in its constructor.

我想了解何时使用ApplicationContext层次结构(而不​​是单个ApplicationContext).

I would like to understand when to use ApplicationContext hierarchies (instead of a single ApplicationContext).

我能从Google那里得到的最好的东西是.我的理解是,如果一个应用程序在各个层上定义了大量的bean,那么每个层都有自己的ApplicationContext将会是一个好处.不能理解的是这样做的好处是什么?如何获得好处?

The best I could get from google was this. And what I understand is that if an application has a large number of beans defined at the various layers then each layer having its own ApplicationContext would be a benefit. What is not understood is what is the benefit of doing so and how is the benefit achieved?

TIA, 维杰

推荐答案

最典型的用例是在单个web应用程序中有多个Spring DispatcherServlet,并且每个servlet都有自己的应用程序上下文,但是需要在它们之间共享bean.在这种情况下,您需要在webapp级别添加第三个上下文,该上下文是每个servlet appcontexts的父级.

The classic use-case for this is when you have multiple Spring DispatcherServlet within a single webapp, with each of these servlets having their own app context, but which need to share beans between them. In this case, you add a 3rd context at the level of the webapp, which is the parent of each of the servlet appcontexts.

例如,如果您将多个Web应用程序捆绑到一个JavaEE EAR中,则可以进一步采用这种模式.在这里,EAR可以具有自己的上下文,该上下文是各个webapp上下文的父级,是servlet上下文的父级,依此类推.您具有这种责任等级.

You can take this pattern further, for example if you have multiple webapps bundled into a single JavaEE EAR. Here, the EAR can have its own context, which is the parent of the individual webapp contexts, which is the parent of the servlet contexts, and so on. You have this hierarchy of responsibility.

在其他情况下,上下文结构由其他因素决定.例如,Spring Security独立于Spring MVC,并要求其配置Bean进入webapp上下文.如果要与之一起使用Spring MVC,则必须将其配置放入servlet上下文中,该servlet上下文具有根webapp上下文作为其父对象.

In other situations, the context structure is dictated by some other factor. For example, Spring Security is independent of Spring MVC, and requires its configuration beans to go in the webapp context. If you want to use Spring MVC with it, then the config for that has to go into the servlet context, which has the root webapp context as its parent.

这篇关于为什么要使用Spring ApplicationContext层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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