gpt4 book ai didi

java - 根据多个 Spring 配置文件定义 bean

转载 作者:搜寻专家 更新时间:2023-11-01 01:33:10 27 4
gpt4 key购买 nike

我的应用程序中有 3 个配置文件,名为:dev、test、prod。我想使用 spring 配置文件来自定义 bean 注入(inject),这样对于配置文件开发和测试我将有一个 bean 实现,对于配置文件生产我将有另一个。问题是如何实现这一目标。我如何设置一个 bean 在两个不同的配置文件中处于 Activity 状态。我试过这样的事情:

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

但不幸的是,spring 将其视为一个名为 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 - 根据多个 Spring 配置文件定义 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942187/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com