【发布时间】:2016-07-30 11:51:39
【问题描述】:
我正在自学 Spring Cloud Config Server,但在将属性注入 bean 时遇到了一些麻烦。
所以我有一个简单的 Spring Boot 应用程序作为配置客户端,仅用于测试:
@SpringBootApplication
@ConfigurationProperties
public class DemoApplication {
@Value("${greeting}")
static private String greeting;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("The greeting is: " + greeting);
}
}
但系统打印:
The greeting is: null
检查 env 端点,我实际上发现 "${greeting}" 属性在环境中:
profiles: [ ],
configService:https://github.com/mstine/config-repo.git/demo.yml: {
greeting: "Webhook"
},
configService:https://github.com/mstine/config-repo.git/application.yml: {
eureka.instance.hostname: "localhost",
eureka.instance.leaseRenewalIntervalInSeconds: 10,
eureka.instance.metadataMap.instanceId: "${vcap.application.instance_id:${spring.application.name}:${server.port:8080}}",
eureka.client.serviceUrl.defaultZone: "${vcap.services.service-registry.credentials.uri:http://127.0.0.1:8761}/eureka/",
foo: "barnyardAnimal"
},
请注意,在configService 中有一个名为greeting 的属性,其值为"Webhook"
我是 Spring Framework 的新手,所以我想知道我是不是搞砸了什么?有人建议我也可以使用Environment 访问外部属性,但我没有找到太多关于如何使用它的教程。有什么想法吗?
====================================更新=========== ============================ 添加配置服务器的代码:
Application.java:
package io.spring.cloud.samples.fortuneteller.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.yml:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/mstine/config-repo.git
【问题讨论】:
-
这个问题你解决了吗?我也有类似的问题!
标签: spring spring-cloud-config