【问题标题】:Inject @Value inside into @PropertySource in spring boot在spring boot中将@Value注入@PropertySource
【发布时间】:2021-02-10 09:04:28
【问题描述】:

我有两个应用程序属性配置文件devprod。我将应用程序属性文件中的配置文件设置为spring.profiles.active=dev

我想根据活动配置文件动态加载我的类路径属性文件。所以我想将@Value 注释注入到我的类路径变量中,如下所示:

@Configuration
@PropertySource({"classpath:application-@Value('${spring.profiles.active:}').properties"})

有可能吗?

【问题讨论】:

  • Spring boot 默认加载配置文件特定的属性文件。无需指定任何 @PropertySource 注释。

标签: java spring spring-boot annotations


【解决方案1】:

您需要有一个文件 application.properties,您必须在其中指定配置文件,例如 spring.profiles.active=dev

并且为您有特定配置的每个配置文件创建专用文件。 应用程序-dev.properties 应用程序-uat.propertiea

你可以使用Value注解,它会被自动拾取。

看到这个:https://dzone.com/articles/spring-boot-profiles-1

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2017-01-15
    • 2021-06-30
    • 2019-04-30
    • 2019-12-15
    • 2021-01-13
    相关资源
    最近更新 更多