Spring-Boot multi module unable to read properties file from another module

徘徊边缘 提交于 2019-12-01 08:54:34

After looking in Mikhail Kholodkov post(Thanks!),

The solution is to add the following annotation to the using service Execution point:

 @PropertySources({
    @PropertySource("classpath:jwtConfig.properties"),
    @PropertySource("classpath:app.properties")
})
public class OtherServiceApplication {
public static void main(String[] args) {
    SpringApplication.run(OtherServiceApplication.class, args);
    }
}

I think instead of using @PropertySources , the better approach and more suitable one would be to use @ComponentScan in your module that contains the "main method". Since you would need an instance of JWTConfiguration class rather than the actual .property file, it is more recommended to expose the bean and make springboot scan it from another module, rather than to expose the property file (because that makes the jwtConfiguration.java file in another module rather useless.) So you could probably try something like this

Say we have two modules - Module1 and Module2 inside the main module (that has only pom). I presume you know the drill that the no-code module just packages the app as "pom" and describes the dependant modules inside

Your main pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>XXX</groupId>
    <artifactId>XXXX</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>ws-cms-engine</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring-boot.version>2.0.0.RELEASE</spring-boot.version>
        <spring-kafka.version>2.2.3.RELEASE</spring-kafka.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
............
............
    <modules>
      <module>Module1</module>
      <module>Module2</module>
    </modules>

Now lets consider that your JWTConfiguration is in Module1 and it uses the property file in the resources folder declared - application.properties

Sample JWTConfiguation.java file

package common.module2.config
@Configuration
@PropertySources("classpath:application.properties")
public class JWTConfiguration{

  @Value("${config1}")
  private String someConfig;


}

Now if your Module2, has the main class that needs to make use of this configuration then probably something like this would make sense

We need to make the SpringBoot container read from the bean declared in module1 rather than read the actual property file

@ComponentScan(basepackages={"common.module2.config", "common.module1.this.config"})
@SpringBootApplication
public class Application(){
    public static void main(String args[]){
     SpringApplication.run(Application.class);
   }
}

So here we inform that beans declared in module2 package needs to be scanned by the spring container when it starts and initializes

Now you can Autowire the bean in your required service and use it

@Service
public class SampleService{
   @Autowired
   JWTConfiguration config;

}

That should autowire the managed instance of JWTConfiguration for you to use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!