Facing issue with dependency variables.
I have some core dependency projects. they have their own properties. now when I use these dependencies in my project, I don't want to define those properties again in my application.properties file. also if I define the same variable in application.properties that will override the dependency properties
for Ex.
DependencyProjectA
-src/main/java
-com.myApp.accessor
ResourceAccessor.java
{
@Value("${projectA.value}")
private String projectValue;
}
-src/test/resources
-projectA.properties
projectA.value: test123
ProjectB (dependent on ProjectA)
-src/main/java
-xyz.java
{
@Value("${projectB.value}")
private String projectValue;
}
-src/test/resources
-application.properties
projectB.value: test123
-pom.xml
<dependency>
DependencyProjectA
</dependency>
Now when I run the projectB jar with the command java -jar target/projectB.jar I'm facing the following issue:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'projectA.value' in value "${projectA.value}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[spring-core-5.2.1.RELEASE.jar!/:5.2.1.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.2.1.RELEASE.jar!/:5.2.1.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-5.2.1.RELEASE.jar!/:5.2.1.RELEASE]
How we can resolve the dependency jar variable with a dependency property file (projectA.properties) instead of defining in projectB property file (application.properties).
If I understood right, your properties files are located in src/main/resources
but not in src/test/resources
, otherwise test properties files are not packing in final jar file. For Spring Boot 2.4.0 or newer, you can use property spring.config.import
Example:
spring.config.import=classpath:projectA.properties
For older Spring Boot version, I used application argument spring.config.additional-location
for running application:
spring.config.additional-location=classpath:projectA.properties
Anyway you can set up PropertySourcesPlaceholderConfigurer
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{new ClassPathResource("projectA.properties")};
pspc.setLocations(resources);
pspc.setIgnoreUnresolvablePlaceholders(true);
return pspc;
}
And there are some additional properties to customize the folders from which the custom properties files will be loaded. Please check: https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html
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.