Java 通过 web.xml 中的 ContextLoaderListener 初始化 spring bean profile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21574551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:26:58  来源:igfitidea点击:

Initialize spring bean profile through ContextLoaderListener in web.xml

javaspringweb.xmlservlet-listenersspring-bean

提问by bachr

In my web.xmlI'm declaring a ContextLoaderListenerto configure spring application this way:

在我的web.xml声明中,我ContextLoaderListener以这种方式配置 spring 应用程序:

<listener>    
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
</listener>

In one of my spring configuration xml files, I'm using different beans profilefor development and production.

在我的一个 spring 配置 xml 文件中,我使用不同的bean 配置文件进行开发和生产。

<beans profile="production">
   <bean />
</beans
<beans profile="development">
   <bean />
</beans

How I could set the default beans profile in the web.xml? is there something similar to the following when using ContextLoaderListenerinstead of spring servlet:

我如何在web.xml? 使用ContextLoaderListener代替spring servlet时是否有类似以下内容:

<init-param>
   <param-name>spring.profiles.active</param-name>
   <param-value>production</param-value>
</init-param>

采纳答案by nobeh

You can configure web.xmlat the level of ContextLoaderListenerwith:

您可以web.xml在以下级别进行配置ContextLoaderListener

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</context-param>

and the level of DispatcherServletwith:

和水平DispatcherServlet

<init-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</init-param>

Reference: http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/

参考:http: //spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/