Spring云配置服务器上的属性不存在';t注入到Spring应用程序中

Spring云配置服务器上的属性不存在';t注入到Spring应用程序中,spring,spring-cloud-config,Spring,Spring Cloud Config,我正在自学SpringCloudConfig服务器,在将属性注入bean时遇到了一些问题 因此,我有一个简单的Spring引导应用程序作为配置客户端,仅用于测试: @SpringBootApplication @ConfigurationProperties public class DemoApplication { @Value("${greeting}") static private String greeting; public static void ma

我正在自学SpringCloudConfig服务器,在将属性注入bean时遇到了一些问题

因此,我有一个简单的Spring引导应用程序作为配置客户端,仅用于测试:

@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框架的新手,所以我想知道我是不是搞砸了什么?有人建议我也可以使用
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

我认为您也需要创建和启动服务器,我没有看到代码

@SpringBootApplication
@EnableConfigServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
启用服务器后,客户端需要连接到服务器并请求服务器为其存储的配置参数。此服务器需要存储要在其中存储值的.yml

客户端代码需要与服务器对话,并使用bootstrap.yml请求特定的值

---
spring:
  profiles:
    active: northamerica
  application:
    name: koitoerclient
  cloud:
     config:
       uri: http://localhost:8001
server:
  port: 8002

您可以在github中找到客户机和服务器代码的配置和示例,非常简单,因此您可以理解整个配置。

据我所知,您忘记添加

@EnableDiscoveryClient

在客户端应用程序的主类之上


我这样做了,它在类似的情况下对我起作用

您不能将属性作为静态字段注入。尝试将它注入到一些@Component中

您需要将
spring云依赖项
添加到客户端应用程序的类路径中,以便它在自动启动配置中使用云配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring.cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

org.springframework.cloud
spring云依赖关系
${spring.cloud.version}
聚甲醛
进口

如果您使用的是本地存储库,
应用程序.yml
应该如下所示:

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file: --Path--

嗨,谢谢你的回答。配置服务器已启动并正在运行,但仍不显示该值。我在更新中附加了配置服务器代码段。谢谢。你解决这个问题了吗?我也有类似的问题!