Spring Cloud Config - Could not resolve placeholder

时光怂恿深爱的人放手 提交于 2019-12-13 06:54:32

问题


Background

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.

Problem

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']]]

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;
    }

}

Stacktrace:

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

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"
            }
        }
    ]
}

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

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.

...so

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


回答1:


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.

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>


来源:https://stackoverflow.com/questions/41164983/spring-cloud-config-could-not-resolve-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!