升级到 Spring Boot 2 后解决 bootstrap.properties 中的占位符问题

Issues in resolving placeholders in bootstrap.properties after upgrading to Springboot 2

Spring 依赖关系:

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

Spring-云依赖:

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

启动时出错 -

2018-03-09 19:41:35.096  WARN 97916 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'propertySourceBootstrapConfiguration': Unsatisfied dependency expressed through field 'propertySourceLocators'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServicePropertySource' defined in org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration: Unsatisfied dependency expressed through method 'configServicePropertySource' parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'configClientProperties': Could not bind properties to 'ConfigClientProperties' : prefix=spring.cloud.config, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.cloud.config.password' to java.lang.String

bootstrap.properties:

spring.cloud.config.failFast=true
spring.profiles.active=${ACTIVE_PROFILES:dev}
spring.cloud.config.uri=${CLOUD_CONFIG_URL:http://domainname/cloud-config}
spring.cloud.config.username=${CLOUD_CONFIG_USERNAME}
spring.cloud.config.password=${CLOUD_CONFIG_PASSWORD}

想通了:

spring.cloud.config.username=${CLOUD_CONFIG_USERNAME}
spring.cloud.config.password=${CLOUD_CONFIG_PASSWORD}

需要改为 -

spring.cloud.config.username=${CLOUD_CONFIG_USERNAME:}
spring.cloud.config.password=${CLOUD_CONFIG_PASSWORD:}

注意末尾的冒号。不过,这以前曾有效。