Spring Cloud Config - 无法解析占位符 [英] Spring Cloud Config - Could not resolve placeholder

查看:86
本文介绍了Spring Cloud Config - 无法解析占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个中型的基于 spring 的应用程序添加集中配置支持.我最近使它成为 Bootiful :D 它现在可以在嵌入式 tomcat 中作为 jar 运行,尽管使用与以前相同的旧配置文件.所以接下来我想摆脱文件系统属性文件.我已经设置并运行了 Config 服务器,并由包含配置的 git 存储库提供支持.

I am trying to add centralized configuration support to a medium sized spring based application. I recently made it Bootiful :D It can now run as a jar in an embedded tomcat though with the same old configuration files as before. So next I want to get rid of the filesystem property-files. I have the Config server setup and working, backed by a git repo containing the configuration.

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

During startup my application indicates that a property source is found on the config server.

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 注释的字段,例如.

The problem is mapping to fields annotated with @Value eg.

@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/default 的请求响应为:

Will not map the property even though a request to http://localhost:8888/myapplication/defaultresponds with:

{
    "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/ 并使用cloud config client dep 并将其设置为获取与我的其他项目相同的属性,一切都按预期工作.这让我认为在我更大的项目中有一些依赖与如何解决属性相冲突.如果我在启动期间调试项目,我可以看到我的 configserver 中的属性在 spring Environment 中,但最终没有在 PropertyPlaceholder 中.

What I have tried

Clean slate

If i go to https://start.spring.io/ and generate a project with the cloud config client dep and set it up to fetch the same properties as my other project everything works as intended. Which leads me to think there is some dependency in my bigger project thats is in conflict with how the properties are resolved. If i debug the project during startup i can se that the properties from my configserver are in the spring Environment but does not end up in the PropertyPlaceholder.

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

Mapping the properties from the configserver to a POJO annotated with @ConfigurationProperties works around the problem. But im not in control of all @Value annotated classes so i cannot use this approach for libs from other teams.

有什么方法可以确保在之前映射了 cloud-config 属性 @Value 注释被解析?

Is there some way to make sure the cloud-config properties are mapped before @Value annotations are resolved?

推荐答案

解决方案是(正如 M.Deinum 指出的那样)移除定义的 <context:property-placeholder/>较旧的 xml 配置文件之一.就我而言,它是从另一个导入的 xml 配置文件导入的.一旦我用 java 配置替换了another.xml"的导入,问题就解决了.

The solution was (as M.Deinum pointed out) to remove a defined <context:property-placeholder /> in one of the older xml configuration files. In my case it was imported from another imported xml configuration file. Once i replaced the import of "another.xml" with java config the issue was resolved.

谢谢@M.Deinum!

Thanks @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>

another.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>

这篇关于Spring Cloud Config - 无法解析占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆