【问题标题】:Spring Cloud Config - Could not resolve placeholderSpring Cloud Config - 无法解析占位符
【发布时间】:2017-05-01 02:30:21
【问题描述】:

背景

我正在尝试将集中配置支持添加到基于弹簧的中型应用程序中。我最近把它做成了 Bootiful :D 它现在可以在嵌入式 tomcat 中作为 jar 运行,尽管使用与以前相同的旧配置文件。所以接下来我想摆脱文件系统属性文件。 我有配置服务器设置和工作,由包含配置的 git repo 支持。

问题

在启动过程中,我的应用程序指示在配置服务器上找到了一个属性源。

2016-12-15 13:16:19,759 [admin] [ INFO] [] config.client.ConfigServicePropertySourceLocator - Fetching config from server at: http://localhost:8888
2016-12-15 13:16:20,186 [admin] [ INFO] [] config.client.ConfigServicePropertySourceLocator - Located environment: name=myapplication, profiles=[default], label=develop, version=b065758e8ea56ff9f9e8773f263da7705b6aac29
2016-12-15 13:16:20,188 [admin] [ INFO] [] bootstrap.config.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='http://configserver@gitserver/config.git/application.properties']]]

问题是映射到带有@Value 注释的字段,例如。

@RestController
public class DemoController {

    @Value("${my.property}")
    private String myProperty;

    @RequestMapping("/")
    public String myproperty() {
        return myProperty;
    }

}

堆栈跟踪:

...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'my.property' in string value "${my.property}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
...

即使对http://localhost:8888/myapplication/defaultresponds 的请求也不会映射属性:

{
    "name": "myapplication",
    "profiles": [
        "default"
    ],
    "label": "develop",
    "version": "b065758e8ea56ff9f9e8773f263da7705b6aac29",
    "state": null,
    "propertySources": [
        {
            "name": "http://configserver@gitserver/config.git/application.properties",
            "source": {
                "my.property": "Test successful"
            }
        }
    ]
}

我尝试过的

清白

如果我转到 https://start.spring.io/ 并使用云配置客户端 dep 生成一个项目并将其设置为获取与我的其他项目相同的属性,那么一切都会按预期工作。这使我认为在我的更大项目中存在一些依赖关系,这与属性的解析方式相冲突。如果我在启动期间调试项目,我可以确定我的配置服务器中的属性在 spring Environment 中,但不会在 PropertyPlaceholder 中结束。

配置属性

将配置服务器中的属性映射到使用@ConfigurationProperties 注释的 POJO 可以解决该问题。但我无法控制所有 @Value 带注释的类,因此我不能将这种方法用于其他团队的库。

...所以

是否有某种方法可以确保在 @Value 注释被解析之前映射 cloud-config 属性?

【问题讨论】:

  • 发布完整的堆栈跟踪。 cloud-config 服务器的属性很早就在特殊的引导上下文中解析。所以它们是可用的。我怀疑你正在做一些你应该做的配置。
  • 这个问题太长了。见:pastebin.com/DSreYFth
  • 这是相当标准的做法。查看您的构建文件和依赖项或重现问题的小样本可能会有所帮助。在加载正常的应用程序上下文之前,将配置服务器值添加到环境中。
  • 从堆栈跟踪来看,您已经配置了自己的PropertySourcesPlaceHolderConfigurer。你不需要它删除它。
  • @M.Deinum 我定义了那个 bean 试图解决这个问题,没有任何区别。删除 bean 时出现同样的错误 :( 我有一些尚未转换的旧 xml 配置文件。这会干扰启动序列中环境映射到 PropertySourcePlaceholder 的位置吗?

标签: spring spring-boot spring-cloud spring-cloud-config


【解决方案1】:

解决方案是(正如 M.Deinum 指出的那样)删除旧 xml 配置文件之一中定义的<context:property-placeholder />。在我的例子中,它是从另一个导入的 xml 配置文件中导入的。一旦我用 java config 替换了“another.xml”的导入,问题就解决了。

感谢@M.Deinum!

AdminConfig.java:

@Configuration
@ImportResource(value = {
        "classpath:/legacy-conf/applicationContext.xml"
})
public class AdminConfig {

    //... Bean definitions
}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    ...other config...
    <import resource="classpath:another.xml"/>

</beans>

另一个.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    ...other config...
    <context:property-placeholder />
</beans>

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 2020-10-05
    • 2017-08-17
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2016-07-24
    相关资源
    最近更新 更多