【问题标题】:spring read properties file from different maven modulespring 从不同的 Maven 模块读取属性文件
【发布时间】:2022-09-27 16:09:18
【问题描述】:
demo-parent:
  --web
    --src
      --main
        --java
          --Application.java
        --resources
          --application.yml
          --application-mysql.yml
   --service
   --common
     --src
       --main
         --java
           --Config.java
         --resources
           --core-application.yml

core-application 中有一些spring properties,例如spring.kafka.properties.bootstrap.servers。我怎样才能让它们进入我的网络模块?

我现在有一个错误FileNotFoundException: class path resource [core-application.yml] cannot be opened because it does not exist

  • 如果公共模块作为依赖项可用,则它是可用的并且可以加载。所以它必须要么不能作为依赖项使用,要么你以错误的方式加载它。
  • @M.Deinum 不,我可以加载模块。但我无法在加载的模块中获取弹簧属性。
  • 正如我所说,它要么不能作为类路径上的模块使用或者你加载错了。所以请展示你如何在类路径中做事(添加模块等)以及你是如何加载它的。

标签: java spring spring-boot maven properties


【解决方案1】:

根据 Java 8 约定,@PropertySource 注释是可重复的。因此,如果我们使用 Java 8 或更高版本,我们可以使用这个注解来定义多个属性位置:

@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
    //...
}



@PropertySources({
    @PropertySource("classpath:foo.properties"),
    @PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
    //...
}

参考这篇文章,https://www.baeldung.com/properties-with-spring

【讨论】:

  • 当我有自己想从属性文件中获取的属性时,这很有效。但这是我不使用的弹簧属性,但弹簧内部使用它们。
猜你喜欢
  • 1970-01-01
  • 2017-02-26
  • 2019-03-11
  • 2013-02-07
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 2011-10-31
相关资源
最近更新 更多