Home > Blockchain >  spring read properties file from different maven module
spring read properties file from different maven module

Time:09-28

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

there are some spring properties in core-application like spring.kafka.properties.bootstrap.servers. How can I get them in my web module?

I have an error FileNotFoundException: class path resource [core-application.yml] cannot be opened because it does not exist now.

CodePudding user response:

The @PropertySource annotation is repeatable according to Java 8 conventions. Therefore, if we're using Java 8 or higher, we can use this annotation to define multiple property locations:

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



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

Refer this article, https://www.baeldung.com/properties-with-spring

  • Related