【问题标题】:Spring boot Custom PropertiesSpring Boot 自定义属性
【发布时间】:2020-10-26 02:29:16
【问题描述】:

在我的项目中有 2 个资源属性

1.应用程序.properties

server.port=8002

spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=

2。 application-development.properties

server.port=8002

spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=

spring.data.solr.host

该类使用开发的值属性

@Configuration
@EnableSolrRepositories(basePackages = {
    "id.alfadigital.alfagift.service.product.v1.db.solr.repository",
    "id.alfadigital.alfagift.service.product.v2.db.solr.repository"
})
public class SolrConfiguration {

  @Value("${spring.data.solr.host}")
  private String solrUrl;

  @Bean
  public SolrClient solrClient() {
    return new HttpSolrClient.Builder(solrUrl).build();
  }

  @Bean
  public SolrTemplate solrTemplate(SolrClient client) {
    return new SolrTemplate(client);
  }
}

我使用 application-development.properties 作为我的项目资源

所以我使用以下命令运行该项目:

mvn spring-boot:run -D spring.profiles.active=development

但运行项目时出现错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solrConfiguration': 
           Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
           Could not resolve placeholder 'spring.data.solr.host' in value "${spring.data.solr.host}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.solr.host' in value "${spring.data.solr.host}"

我很困惑,我的错误在哪里,我该怎么办?

【问题讨论】:

  • 您是否检查过您的应用程序是否在开发配置文件下运行。似乎不是。如果您到达 spring 日志的开头,您可以看到活动配置文件。
  • 您的开发配置中该属性实际上是否存在值?因为在你分享的属性文件中,没有。

标签: java spring-boot


【解决方案1】:

确保您的属性文件与 here 所述的 Spring 配置文件的名称匹配。

也就是说,如果您从“开发”配置文件运行,Spring 应该选择application-development.properties 文件(或application-development.yml)。

然后在您的application.properties 文件中,您可以使用spring.profiles.active=development 指定您的个人资料。或者您可以使用-Dprofile 从命令行指定配置文件,正如您所提到的。

如链接中所述,“如果指定了多个配置文件,则应用最后获胜策略。例如,由 spring.profiles.active 属性指定的配置文件添加在通过 SpringApplication API 配置的配置文件之后,并且因此优先。"

但还要注意,在您的共享代码中,您的 spring.data.solr.host 属性没有任何价值。

【讨论】:

    【解决方案2】:

    如果您有正确的文件名application-development.properties 和正确的Java Opts -Dspring.profiles.active=development,您还必须将配置文件特定的属性文件与application.properties 放在一起

    配置文件特定的属性从与标准 application.properties 相同的位置加载

    https://docs.spring.io/spring-boot/docs/2.1.12.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

    【讨论】:

      【解决方案3】:

      您能否使用以下命令运行您的应用程序。由于错误使用命令,无法获取development 配置文件。

      mvn spring-boot:run -Dspring.profiles.active=development

      示例: how to use Spring Boot profiles

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-14
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        相关资源
        最近更新 更多