Spring boot properties file boot from another maven module

My maven project has three modules: web , service , common

Some parts of my project:

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

Since I want to use this application-errors.properties module from web , 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 am adding EnableConfigurationProperties to Application.java which contains the main method and I can get the key and value from the application-errors.properties file, my question is:

If I want to call appliction-errors.propertie in another module , as a service, and the service does not have a main method , what should I do?

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

All tips are welcome, thanks in advance.

+8
java properties spring-boot spring-mvc
source share
1 answer

Instead of accessing the -error.properties applications, you can automatically connect the ErrorCode class to your services in other modules (provided that the ErrorCode is present in the assembly class path of these 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; } } 

Automatic posting of a configuration component in a 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 } } 

Turn on automatic tuning 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(); } } 

According to Javadocs EnableConfigurationProperties ,

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

Thus, all beans defined by the ConfigurationProperties annotation are detected in the application context and can be automatically connected to other services.

+4
source share

All Articles