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.
java properties spring-boot spring-mvc
diligent
source share