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

Roie Beck picture Roie Beck · Oct 5, 2018 · Viewed 8.6k times · Source

I have searched High and low and still I am unable to find a simple answer to this very annoying problem,

I have followed this great guide: JWT with multi service app Everything works great but in the end of the guide we are suggested to create a config-service(module) , which I have done.

The problem is that I am unable to override the default configuration of JwtConfig class

The project structure is as follows:

-config-service 

    | JwtConfig.java
     \
        | resources 
        \
         | jwtConfig.properties

 -other-service (add dependency in the pom file of the config-service)
     |
       someOtherclass.java (import the JwtConfig class & using @Bean to initialize )

The JwtConfig class:

/*all the imports*/ 
@PropertySource(value = "classpath:jwtConfig.properties")
public class JwtConfig {

@Value("${security.jwt.uri:/auth/**}")
private String Uri;

@Value("${security.jwt.header:Authorization}")
private String header;

@Value("${security.jwt.prefix:Bearer }")
private String prefix;

@Value("${security.jwt.expiration:#{24*60*60}}")
private int expiration;

@Value("${security.jwt.secret:JwtSecretKey}")
private String secret;

 //getters

The someOtherclass.java:

/*imports*/

@Configuration
@EnableWebSecurity
public class SecurityCredentialsConfig  extends WebSecurityConfigurerAdapter 
{ 

   private JwtConfig jwtConfig; 

   @Autowired
   public void setJwtConfig(JwtConfig jwtConfig) {
       this.jwtConfig = jwtConfig;
   }
   @Bean
   public JwtConfig jwtConfig() {
    return new JwtConfig();
   }
   /*other code*/

The problem is that it does not matter what parameters I put in the jwtConfig.properties file,

For example:

   security.jwt.uri=test 

It will not appear in the JwtConfig bean when the other service loads it.

Only the default @Value's are loaded.

can someone have any advice? how may I fix it? Many thanks!

Answer

Roie Beck picture Roie Beck · Oct 6, 2018

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);
    }
}