Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 432
diligent Avatar asked Nov 30 '15 09:11

diligent


People also ask

Can we have two properties file in Spring Boot?

properties” property file, as it was automatically built inside the Spring boot application when the project is initially created. We can create and maintain multiple property files within “Resources” module, where we will be able to define different types of property values in different files.

What is the correct way to access multiple properties in Spring?

You can then place @EnableConfigurationProperties(classes = MyProperties. class) on your main application class or another @Configuration and then @Autowire your MyProperties class where its needed.


1 Answers

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.

like image 151
Mohit Avatar answered Sep 28 '22 13:09

Mohit