简体   繁体   中英

spring boot read properties file from different maven module

My maven project have three modules, web , service , common

Some parts of my project like this:

demo-parent:
  --web
    --src
      --main
        --java
          --Application.java
        --resources
          --application.properties
          --application-mysql.properties
   --service
   --common
     --src
       --main
         --java
           --ErrorCode.java
         --resources
           --application-errors.properties

In web moudle Application.java , I want to read contents from common moudle application-errors.properties .

Here is my ErrorCode.java in common :

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(locations = "classpath:application-errors.properties")
public class ErrorCode {
    private int code;
    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Because I want to use this application-errors.properties from web module, So in web moudle, in Application.java :

@SpringBootApplication
@PropertySources({
    @PropertySource("application-mysql.properties"),
    @PropertySource("application-errors.properties")
})
@EnableConfigurationProperties({ErrorCode.class})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.run(args);
    }
}

Now, I add EnableConfigurationProperties to Application.java which contains main method, and I can get key and value from application-errors.properties file, my question is :

If I want to invoke appliction-errors.propertie in other module like service, and service do not have a main method , what should I do ?

We can read properties file by using java Properties class, but I want to use spring way .

Anything advice is welcome, thanks in advance.

Instead of accessing application-error.properties, you can auto wire ErrorCode class in your services in other modules (assuming the ErrorCode is present on the build classpath of those services).

First define your error message configuration component.

@Configuration
@ConfigurationProperties(locations = "classpath:application-error.properties")
public class ErrorCode {

    private int code;
    private String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Auto wire the configuration component in service

@Component
public class Service {

    @Autowired
    private ErrorCode errorCode;


    public void accessErrorCode() {
        System.out.println(errorCode.getCode()); //Print code property
        System.out.println(errorCode.getMessage()); //print message property
    }

}

Enabling auto configuration in spring application

@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplicationBuilder(Application.class).web(false).application();
        ConfigurableApplicationContext context = application.run(args);
        Service s = context.getBean(Service.class);
        s.accessErrorCode();
    }
}

As per the Javadocs of EnableConfigurationProperties ,

{@link ConfigurationProperties} beans can be registered in the standard way (for * example using {@link Bean @Bean} methods) or, for convenience, can be specified * directly on this annotation.

Thus, all beans defined with annotation ConfigurationProperties are detected under application context and can be auto-wired in other services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM