I am trying to find the correct and standard way of putting application.properties Files in a Springboot multi-module project and deploying as as JAR file I have a Springboot project with two modules- library and application. Library contains only a utility service which needs to be consumed by application(main) module. In the library module I have put application.properties files which contains a key- values pairs:
service.message.test=Hello from Library Module
I am using this property in one of the service class file in library module using the below code:
@Service
@EnableConfigurationProperties(ServiceProperties.class)
public class MyService {
@Value("${service.message.test}")
private String libMessage;
private final ServiceProperties serviceProperties;
public MyService(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
}
public String message() {
return this.serviceProperties.getMessage()+libMessage; };
}
Also, i have a application module(main) which also has a application.properties files in the src/main/resources folder.This file contains other key-value pairs for application
service.message=Hello, World
Now i am using the mvn clean package command to package the app in a JAR file. But on running the command, i am getting the error as :
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'service.message.test' in value "${service.message.test}"
Now, I have the following questions:
If we are creating SpribgBoot mullti-module project,can't we keep application.properties in each module but rather should always use @ConfigurationProperties("service") only?
I need to pass several properties to various classes in the library module .I am doing this by passing it from properties files at module level using @Value("${xxxx}”) annotation .Since, its giving error, is this not allowed?
Is application.properties file only allowed in application(main) module and not in the dependent individual modules?
What is the standard way of managing properties files in Springboot multi-module project ?
I am trying to find the correct and standard way of putting application.properties Files in a Springboot multi-module project and deploying as as JAR file I have a Springboot project with two modules- library and application. Library contains only a utility service which needs to be consumed by application(main) module. In the library module I have put application.properties files which contains a key- values pairs:
service.message.test=Hello from Library Module
I am using this property in one of the service class file in library module using the below code:
@Service
@EnableConfigurationProperties(ServiceProperties.class)
public class MyService {
@Value("${service.message.test}")
private String libMessage;
private final ServiceProperties serviceProperties;
public MyService(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
}
public String message() {
return this.serviceProperties.getMessage()+libMessage; };
}
Also, i have a application module(main) which also has a application.properties files in the src/main/resources folder.This file contains other key-value pairs for application
service.message=Hello, World
Now i am using the mvn clean package command to package the app in a JAR file. But on running the command, i am getting the error as :
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'service.message.test' in value "${service.message.test}"
Now, I have the following questions:
If we are creating SpribgBoot mullti-module project,can't we keep application.properties in each module but rather should always use @ConfigurationProperties("service") only?
I need to pass several properties to various classes in the library module .I am doing this by passing it from properties files at module level using @Value("${xxxx}”) annotation .Since, its giving error, is this not allowed?
Is application.properties file only allowed in application(main) module and not in the dependent individual modules?
What is the standard way of managing properties files in Springboot multi-module project ?
1 Answer
Reset to default 1In multi-module Spring (Maven or Gradle) you can achieve what you want, but there is a gotcha with the application.properties
file... each module has its own and only the "outer" one wins.
So if you have some properties that you want to specify in the library, use a different name. I suggest you use the convention of Spring (Active) Profiles.
In the Library name the file application-library.properies
Then in the main application specify the Active Profile as library
and Spring will load whatever is in the main module's application.properties
AND what is found in application-library.properies
.
There are several ways to specify active profiles, one is simply on the execute of the outer application (e.g. in your IDE's Run configuration), see Set the Active Spring Profiles for many other ways
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630028a4637048.html
application.properties
is on the classpath Spring will only find the one from the application as that is the closest to the class that it is being loaded from. You should put all the properties in this, theapplication.properties
in the jars more or less contain defaults, hardcode these into your classes and allow them to be overridden with the mainapplication.properties
. – M. Deinum Commented Nov 18, 2024 at 7:59