【问题标题】:How to set a Spring profile to a package?如何将 Spring 配置文件设置为包?
【发布时间】:2014-11-28 09:59:51
【问题描述】:

我想为整个包设置配置文件名称,但我不知道如何操作。如果 where 不是简单的方法,那么我必须用 @Profile 注释标记包和子包中的每个类。

<context:component-scan/> 标签不支持profile 这样的属性,所以我不知道。

【问题讨论】:

    标签: java spring spring-profiles


    【解决方案1】:

    如果您不混合使用 XML 和 Java 配置,您可以在目标包中有 @ComponentScan 注释的 bean 上使用 @Profile 吗?

    与 XML 类似:您可以有两个不同的 <beans ...> 部分,每个部分具有不同的配置文件,并且在每个部分中您定义自己的 <context:component-scan basePackage="..." />

    @Configuration
    @Profile("profile1")
    @ComponentScan(basePackage="package1")
    class Config1 {
    }
    
    @Configuration
    @Profile("profile2")
    @ComponentScan(basePackage="package2")
    class Config2 {
    }
    

    【讨论】:

    • 而且,我们可以在application.properties中指定活动配置文件:spring.profiles.active=profile1
    猜你喜欢
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2017-08-04
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多