Java 有没有办法在 applicationContext.xml 文件中启用或禁用 Spring bean 定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8279270/
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-15 00:36:15  来源:igfitidea点击:

Is there any way to enable or disable the Spring bean definition in applicationContext.xml file?

javaspringspring-mvc

提问by user1065374

Is there any way to enableor disablea java bean definition in application context?

有没有办法在应用程序上下文中启用禁用java bean 定义?

<bean id="enBean" classs="com.en.bean.BeanName">
   <property name="prop1"/>
</bean>

Or, is there any way to load the bean conditionallydefined in application context?

或者,有什么方法可以有条件地加载应用程序上下文中定义的 bean ?

回答by Jigar Joshi

There is a new feature @Profilein spring 3.1 that would do the job

@Profilespring 3.1 中有一个新功能可以完成这项工作

From here

这里

Spring 3.1 introduces the concept of environment profiles. A common use case is the setting up of beans that are different between development, QA and production environments. A typical example is going against a standalone DataSource in development versus looking up the DataSource from JNDI in production. Another example is a beans profile for profiling that can easily be turned on or off. You can add a profile attribute on a beans element in XML or add @Profile annotation in code. Note that a Spring bean can be assigned to multiple profiles.

Spring 3.1 引入了环境配置文件的概念。一个常见的用例是在开发、QA 和生产环境之间设置不同的 bean。一个典型的例子是在开发中使用独立的数据源,而不是在生产中从 JNDI 查找数据源。另一个示例是可以轻松打开或关闭的用于分析的 bean 配置文件。您可以在 XML 中的 beans 元素上添加 profile 属性或在代码中添加 @Profile 注释。请注意,一个 Spring bean 可以分配给多个配置文件。

<beans profile="dev">
    ...
</beans>
@Profile("dev")
public class Bean {
    ...
}

These profiles can be activated through the spring.profiles.active property which may be specified through an environment variable, a JVM system property, a Servlet in web.xml or JNDI. These profiles can also be activated through code using Environment.setActiveProfiles(String ...). To make bean profiles work, nested beans elements are now allowed in the Spring XML, although constrained only at the end of the file. Note that it's recommended to keep your bean topology as close as possible between environments, so your application gets properly tested across environments. You also use the Environment.containsProperty() method to search for properties across the different property sources. This property resolution also works for ${placeholder} variables in XML bean definitions.

这些配置文件可以通过 spring.profiles.active 属性激活,该属性可以通过环境变量、JVM 系统属性、web.xml 或 JNDI 中的 Servlet 指定。这些配置文件也可以通过使用 Environment.setActiveProfiles(String ...) 的代码激活。为了使 bean 配置文件起作用,Spring XML 中现在允许嵌套 bean 元素,尽管仅在文件末尾受到限制。请注意,建议在不同环境之间保持 bean 拓扑尽可能接近,以便您的应用程序在不同环境中得到正确测试。您还可以使用 Environment.containsProperty() 方法在不同的属性源中搜索属性。此属性解析也适用于 XML bean 定义中的 ${placeholder} 变量。