[英]how to access dependency resource propeties in spring-boot project
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.
现在当我在我的项目中使用这些依赖项时,我不想在我的 application.properties 文件中再次定义这些属性。 also if I define the same variable in application.properties that will override the dependency properties
此外,如果我在 application.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:现在,当我使用命令java -jar target/projectB.jar运行 projectB jar 时,我面临以下问题:
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).我们如何使用依赖属性文件(projectA.properties)而不是在 projectB 属性文件(application.properties)中定义来解析依赖 jar 变量。
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.如果我理解正确,您的属性文件位于
src/main/resources
但不在src/test/resources
,否则测试属性文件不会打包在最终的 jar 文件中。 For Spring Boot 2.4.0 or newer, you can use property spring.config.import
对于 Spring Boot 2.4.0 或更新版本,您可以使用属性
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 Boot 版本,我使用应用程序参数
spring.config.additional-location
来运行应用程序:
spring.config.additional-location=classpath:projectA.properties
Anyway you can set up PropertySourcesPlaceholderConfigurer
无论如何你可以设置
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
请检查: https : //docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.