Spring:可以有两个上下文 - web 和 web后端?
我有一个基于 spring 的网络应用程序,带有基于 spring 的后端。目前,集成是通过手动将后端 .xml 复制到 Web 应用程序资源并合并的愚蠢方式完成的。
我想知道 Spring 是否准备好拥有一个 bean(它是另一个 ApplicationContext ),以及它是否可以从中获取 bean;另外,如果它处理 bean 名称冲突 - 例如,如果我可以为“导入的”bean 分配一个名称空间。
或者 - 这种情况的最佳实践是什么?
谢谢, 安德拉。
I have a spring-base web app with a spring-based backend. Currently, the integration is done in a stupid way of manual copying the backend .xml's to the web app resources and merging.
I was wondering whether Spring is ready to have a bean which would be another ApplicationContext
, and whether it can get beans from it; also if it handles bean name collisions - e.g. if I can assign a namespace to the "imported" beans.
Or - what is the best practice for this case?
Thanks,
Ondra.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我总是按层拆分 Spring XML:Web、服务、持久性。它绝不只存在于一个文件中。我想说这样做是不行的;这是一个最佳实践。
I always split my Spring XML by layer: web, service, persistence. It's never just in one file. I'd say it's beyond okay to do that; it's a best practice.
默认情况下,您应该已经有两个上下文。以 servlet ( [servlet-name]-context.xml ) 命名的上下文是一个 WebApplicationContext,并且是主 ApplicationContext 的子上下文,它是根据 contextConfigLocation 中列出的文件创建的,并由 ContextLoaderListener 加载。子级可以访问父级中定义的任何 Bean,但父级无法访问子级中定义的 Bean,因此在开始移动 Bean 之前请记住这一点。
我只在 WebApplicationContext 中保留特定于 Web 的配置 - 我的控制器和视图等。任何非特定于 Web 的内容都会进入主 ApplicationContext,它本身始终是一个文件,仅导入许多其他应用程序上下文 xml 文件,按照其他人的建议按层细分。
请注意,spring 只允许在每个应用程序上下文中使用单个 property-placeholder 元素,但您也可以在每个子应用程序上下文中使用一个 property-placeholder 元素,因此我的 [servlet-name]-context.xml 文件始终具有一个带有 web- 的 property-placeholder特定的配置属性和主应用程序上下文有不同的属性,我通常在所有导入之前在顶级文件中定义它。
By default, you should already have two contexts. The context named after your servlet ( [servlet-name]-context.xml )is a WebApplicationContext and is a child context of the main ApplicationContext, which is created from the files listed in the contextConfigLocation and loaded by the ContextLoaderListener. The child can access any beans defined in the parent, but the parent has no access to beans defined in the child, so keep that in mind before you start moving beans around.
I keep only web-specific configuration in the WebApplicationContext - my controllers and views and such. Anything that isn't specific to the web goes into the main ApplicationContext, which is itself always a single file which just imports a number of other application context xml files, broken down by layer, as suggested by others.
Note, spring only allows a single property-placeholder element in each application context, but you can have one in each child app context as well, so my [servlet-name]-context.xml file always has a property-placeholder with web-specific config properties and the main application context has a different one, which I usually define right in the top level file, before all of the imports.
如果我没理解错的话,这绝对没问题。有些人将数据库上下文与 Web 上下文分开。 web.xml 中的示例如下:
至于 beans 的冲突 - 您应该为 beans 提供唯一的 ID。
If I've understood you correctly, this is definitely OK. Some people split their database contexts with their web contexts. Example below in your web.xml:
As for the collision of beans - you should be giving beans a unique ID.