Spring 3.1中的默认配置文件 [英] Default profile in Spring 3.1
问题描述
在我的应用程序中,我使用 @Profile(prod)
和 @Profile(demo)注释bean
。
第一个,你可以猜到:),用于连接到生产数据库的bean,第二个用于注释使用一些假数据库的bean( HashMap
或无论如何) - 让开发更快。
In my application I have beans annotated with @Profile("prod")
and @Profile("demo")
.
The first one, as you can guess :), is used on beans that connect to production DB and second one annotates beans that use some fake DB (HashMap
or whatever)- to make development faster.
我想要的是默认配置文件(prod
)如果没有被 something-else 覆盖,它将被永远使用。
What I would like to have is default profile ("prod"
) that will be used always if it is not overridden by "something-else".
完美将是我的 web.xml
:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
然后用 -Dspring.profiles.active =demo覆盖它
以便我可以这样做:
and then override this with -Dspring.profiles.active="demo"
so that I could do:
mvn jetty:run -Dspring.profiles.active="demo".
但遗憾的是这不起作用。任何想法我怎么能得到那个?在我的所有环境中设置 -Dspring.profiles.active =prod
不是一种选择。
But sadly this is not working. Any idea how could I achive that? Setting -Dspring.profiles.active="prod"
on all my environments is not an option.
推荐答案
我的经验是使用
@Profile("default")
如果没有识别出其他配置文件,则只会将bean添加到上下文中。如果您传入不同的个人资料,例如 -Dspring.profiles.active =demo
,此个人资料将被忽略。
the bean will only be added to the context if no other profile is identified. If you pass in a different profile, e.g. -Dspring.profiles.active="demo"
, this profile is ignored.
这篇关于Spring 3.1中的默认配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!