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

后端 未结 2 451
情歌与酒
情歌与酒 2021-01-12 04:05

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 ap

2条回答
  •  -上瘾入骨i
    2021-01-12 04:24

    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

    
    
        4.0.0
        XXX
        XXXX
        pom
        1.0
        ws-cms-engine
        http://maven.apache.org
        
            2.0.0.RELEASE
            2.2.3.RELEASE
        
        
            
                
                    
                    org.springframework.boot
                    spring-boot-dependencies
                    ${spring-boot.version}
                    pom
                    import
                
    ............
    ............
        
          Module1
          Module2
        
    

    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

提交回复
热议问题