【问题标题】:how to access dependency resource propeties in spring-boot project如何在spring-boot项目中访问依赖资源属性
【发布时间】:2020-11-02 15:56:58
【问题描述】:

面临依赖变量的问题。

我有一些核心依赖项目。他们有自己的属性。现在当我在我的项目中使用这些依赖项时,我不想在我的 application.properties 文件中再次定义这些属性。如果我在 application.properties 中定义相同的变量,它将覆盖依赖属性

例如

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>

现在,当我使用命令 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]

我们如何使用依赖属性文件 (projectA.properties) 解析依赖 jar 变量,而不是在 projectB 属性文件 (application.properties) 中定义。

【问题讨论】:

  • 这能回答你的问题吗? Priority of Various Sources in PropertySources
  • @OrangeDog 我如何在这里使用 PropertySource,我也不想更改我的核心模块
  • 你不能改变你的应用程序的行为而不改变它的代码。
  • 我可以更改 ProjectB 而不是 ProjectA :-(
  • 所以在ProjectB中添加一个PropertySource。

标签: java spring-boot maven application.properties


【解决方案1】:

如果我理解正确,您的属性文件位于src/main/resources 而不是src/test/resources,否则测试属性文件不会打包到最终的 jar 文件中。 对于 Spring Boot 2.4.0 或更高版本,您可以使用属性 spring.config.import

例子:

spring.config.import=classpath:projectA.properties

对于较旧的 Spring Boot 版本,我使用应用程序参数 spring.config.additional-location 来运行应用程序:

spring.config.additional-location=classpath:projectA.properties

随便设置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;
    }

还有一些额外的属性可以自定义加载自定义属性文件的文件夹。 请查收:https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多