Spring:如何组织应用程序+ Web上下文? [英] Spring: How should application+web contexts be organized?

查看:116
本文介绍了Spring:如何组织应用程序+ Web上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:


  1. 启动应用并在main中加载 ClassPathXmlApplicationContext ( ),这将启动一个Jetty webapp。

  2. 调度程序servlet然后加载 XmlWebApplicationContext

  3. 调度程序servlet然后查找 WEB-INF / applicaitonContext.xml 作为根上下文加载

  1. Start the app and load ClassPathXmlApplicationContext in main(), this starts a Jetty webapp.
  2. dispatcher servlet then loads a XmlWebApplicationContext
  3. dispatcher servlet then looks for WEB-INF/applicaitonContext.xml to load as the root context

我只是想绕过上下文。

I'm just trying to get my head around contexts.


  • 我应该使用上面列出的3个上下文吗?

  • 我应该使用第一个上下文作为根上下文并且不允许调度程序加载默认的 WEB-INF / applicationContext

  • 我是否应该创建父/子关系在第1和第3个背景之间? (这是由调度员servlet在#2和#3上下文之间完成的)

我还不是很清楚这些上下文如何相互关联,我只需要快速了解它。

I'm just not really clear yet about how these contexts relate to one another, I just need a quick rundown on it.

推荐答案

上下文#1未连接到其他上下文,它只是你如何启动你的网络服务器(Jetty)的实现细节。
上下文#2和#3在Spring中有所解释参考文档

Context #1 is not connected to other contexts at all, it is just an implementation detail of how you start up your webserver (Jetty). Contexts #2 and #3 are somewhat explained in Spring reference documentation.


  • 上下文#2从 WEB-INF / [ servlet的名称] -servlet.xml后缀。由于可以有许多DispatcherServlet,因此对于不同的servlet,单个webapp中可能存在多个这样的上下文。

  • 上下文#3通常从 WEB-加载INF / applicationContext.xml ,你必须采取特殊步骤才能加载它(使用 ContextLoaderListener )。此加载的上下文成为特定Web应用程序中所有特定servlet上下文的父上下文(共享)。因此,它适用于加载业务服务bean和数据库访问bean。

  • Context #2 is loaded from WEB-INF/[servlet-name]-servlet.xml. As there can be many DispatcherServlets, there may be more than one such a context in single webapp, for different servlets.
  • Context #3 is typically loaded from WEB-INF/applicationContext.xml and you have to take special steps to have it loaded (use ContextLoaderListener). This loaded context becomes the parent context (shared) of all specific servlet contexts in particular web application. As such, it is suitable for loading business service beans and database access beans.

您概述的设置完全正常。事实上,我将其称为推荐的设置,因为它使事情变得简单并且接近于在典型的webapp中构建Spring上下文的方式。

The setup you outlined is perfectly OK. In fact, I would call it the recommended setup, as it keeps things simple and close to the way Spring contexts are constructed in a typical webapp.

但是:

如果您不想将业务bean放在单独的上下文中,您可以摆脱上下文#3 。但是,我建议你将它们分开(你可能需要稍后将它们移到另一台机器上并通过某种远程机制提供)。

You may get rid of context #3 if you do not want to keep your business beans in separate context. However, I would recommend you to keep them separate (you might need to move them to a different machine later on and make available over some sort of remoting mechanism).

另一个理由摆脱上下文#3 :您可能希望在多个Web应用程序之间共享您的业务bean。为了实现这一点,你需要一个特殊的Spring ContextLoader子类,然后在Jetty启动你的webapps时做一些魔术。我已经这样做了,如果需要可以提供一些建议。

Another reason to get rid of context #3: you might want to share your business beans among several webapps. The in order to achieve this you would need a special subclass of Spring ContextLoader and then do some magic while Jetty is starting up your webapps. I have done this and can provide some advice, if needed.

最后,你可以摆脱上下文#1 并用旧的替换它-stchool纯java代码,它将引导Jetty。这个决定100%取决于你和偏好的问题。为了记录,我还想使用一个单独的Spring applicationContext来引导Jetty。

Finally, you may get rid of context #1 and replace that with old-school pure java code that would bootstrap Jetty. This decision is 100% up to you and matter of preference. For the record, I also like to use a separate Spring applicationContext for bootstrapping Jetty.

这篇关于Spring:如何组织应用程序+ Web上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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