例如,我有很多配置文件,"dev","prod","test"等。
interface A
@Component
class DefaultImpl implements A
@Profile("test")
@Component
class TestImpl implements A
我只希望 TestImpl 用于配置文件"test",但 DefaultImpl 用于所有其他配置文件。
更新:为什么@Profile("默认")对我不起作用:
我有两个测试配置文件,即"test1"和"test2"
我在配置文件"test1"中提供了不同的实现:
@Profile("default")
class DefaultImpl extends A
@Profile("test1")
class Test1Impl extends A
现在当我@ActivateProfile("test2")时,它不会选择 DefaultImpl
但是,如果我不设置配置文件,如下所示:
class DefaultImpl extends A
@Profile("test1")
class Test1Impl extends A
配置文件"test2"最终将有两个豆子,并且不知道该连接哪个。
现在,只有这样才能工作:
@Profile("test2", "prod", ....)
class DefaultImpl extends A
@Profile("test1")
class Test1Impl extends A
除了在 DefaultImpl 中添加所有其他配置文件名称之外,我还能做什么吗?
如果您不指定配置文件,您将获得"默认"配置文件。
因此,在您的示例中,如果您不使用配置文件,则将加载 DefaultImpl。 如果将配置文件设置为使用 :
@ActiveProfiles("test") or -Dspring.profiles.active=test
您将获得机器人 DefaultImpl 和 TestImpl
您可以更改以确保 DefaultImpl 不会运行进行测试:
@Profile("default")
@Component
class DefaultImpl implements A
在 DefaultImpl 上使用 @Profile("default")
。
或者,您也可以创建一个特定的命名默认值并在您的 Web 中指定它.xml:
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>mydefault<param-value>
</context-param>