我正在自学 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
但我没有找到太多关于如何使用它的教程。任何想法?
====================================更新============= ======================== 添加配置服务器的代码:
应用程序.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);
}
}
应用程序.yml:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/mstine/config-repo.git