简体   繁体   English

如何指定bean将在Spring中注册的默认配置文件?

[英]How to specify the default profile that beans will be registered under in Spring?

Given: 鉴于:

<beans ... namespace decelerations>
     <bean id="foo" class="com.example.foo" />
     <beans profile="abc">
         <bean id="bar" class="com.exmaple.bar" />
     </beans>
</bean>

what is the name of the profile that foo is registered under? foo注册的个人资料的名称是什么? Is there a way to override foo in another profile definition? 有没有办法在另一个配置文件定义中覆盖foo? Is there a name for the default profile in spring when a profile is not explicitly specified. 如果未明确指定配置文件,是否在spring中有默认配置文件的名称。

Default profile in spring is "default", see this: https://jira.springsource.org/browse/SPR-8203 spring中的默认配置文件是“default”,请参阅: https//jira.springsource.org/browse/SPR-8203

You can change the default profile in web.xml by doing this: 您可以通过执行以下操作更改web.xml中的默认配置文件:

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>production</param-value>
</context-param>

Command line: 命令行:

-Dspring.profiles.default=production

Env variable: 环境变量:

export spring_profiles_default=production

If active profile is set, it overrides the default. 如果设置了活动配置文件,则它将覆盖默认配置文件。

Foo is simply registered under no profile, it will always be instantiate regardless of the profile you are using in that environment. Foo只是在没有配置文件的情况下注册,无论您在该环境中使用的配置文件如何,它都将始终实例化。 Spring only allow to create multiple beans within an XML file with the same id if they are in different <beans> sets so I don't think that is possible to overwrite the Foo bean if it is not inside a <beans> tag with a profile. Spring只允许在具有相同idXML文件中创建多个bean,如果它们位于不同的<beans>集中,那么如果它不在一个带有a <beans><beans>标记内,我认为不可能覆盖Foo bean。轮廓。

If no profile are defined, Spring will use the profile named default . 如果没有定义配置文件,Spring将使用名为default的配置文件。 But, a bean that is not inside a <beans> tag with a profile will not be registered under that profile. 但是,一bean是不是里面<beans>标签与配置文件将不被该配置文件下注册。 It only means that if you have something like the following XML and that no profiles are provided, beans with the default profile will be loaded as well as beans with no profile. 它只表示如果你有类似下面的XML并且没有提供配置文件,那么将加载具有默认配置文件的bean以及没有配置文件的bean。

<beans ... namespace decelerations>
     <bean id="foo" class="com.example.foo" />
     <beans profile="default">
         <bean id="bar" class="com.exmaple.bar" />
     </beans>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM