java – 为多个弹簧配置文件定义bean

我的应用程序中有3个配置文件名为:dev,test,prod.
我想使用spring配置文件来自定义bean注入,这样对于profile dev和test我将有一个bean实现,而对于profile prod,我将有另一个.
问题是如何实现这一目标.如何设置一个bean在两个不同的配置文件中处于活动状态.
我试过这样的事情:

@Component
@Profile("dev, test")
class DevTestBean{}

但不幸的是,春天将它视为一个名为dev comma space test的单一配置文件.

最佳答案
你必须改为@Profile({“dev”,“test”})

该值必须声明为Set.
documentation

If a @Configuration class is marked with @Profile, all of the @Bean
methods and @Import annotations associated with that class will be
bypassed unless one or more of the specified profiles are active. This
is analogous to the behavior in Spring XML: if the profile attribute
of the beans element is supplied e.g., , the
beans element will not be parsed unless at least profile ‘p1’ or ‘p2’
has been activated. Likewise, if a @Component or @Configuration class
is marked with @Profile({“p1”, “p2”}), that class will not be
registered or processed unless at least profile ‘p1’ or ‘p2’ has been
activated.

转载注明原文:java – 为多个弹簧配置文件定义bean - 代码日志