Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在spring boot中注入用户提供的vcap_服务_Spring_Spring Boot_Cups - Fatal编程技术网

如何在spring boot中注入用户提供的vcap_服务

如何在spring boot中注入用户提供的vcap_服务,spring,spring-boot,cups,Spring,Spring Boot,Cups,我正在spring boot中开发一个应用程序,并在PCF(Pivotal Cloud Foundry)中部署 我已经创建了3个“用户提供的”服务,我想使用@ConfigurationProperties将它们注入到我的代码中。我一直在四处寻找,但我发现的唯一示例是向类中注入服务,我需要注入一个服务列表 我尝试了@ConfigurationProperties(vcap.services),但它不起作用。映射的类为null。你能帮我了解一下这些杯子是如何在弹簧靴中注射的吗?当您以以下方式创建用户

我正在spring boot中开发一个应用程序,并在PCF(Pivotal Cloud Foundry)中部署

我已经创建了3个“用户提供的”服务,我想使用
@ConfigurationProperties
将它们注入到我的代码中。我一直在四处寻找,但我发现的唯一示例是向类中注入服务,我需要注入一个服务列表


我尝试了
@ConfigurationProperties
(vcap.services),但它不起作用。映射的类为
null
。你能帮我了解一下这些杯子是如何在弹簧靴中注射的吗?当您以以下方式创建用户提供的服务时,请提前感谢

cf cups ups-example1-p'{“user”:“user1”,“password”:“password1”}'

并将其绑定到您的应用程序,用户提供的服务中提供的信息将映射到您的
VCAP\u服务
环境变量中

它应该看起来像

{
  "user-provided": [
   {
    "credentials": {
     "password": "password1",
     "user": "user1"
    },
    "label": "user-provided",
    "name": "ups-example1"
   }
  ]
}
在Springs的帮助下,它被映射到一个环境属性中,该属性可由
vcap.services.ups-example1.credentials
访问

要将这些属性映射到Java对象,可以使用
@ConfigurationProperties

@Configuration
@ConfigurationProperties("vcap.services.ups-example1.credentials")
public class UserProvidedServiceOneProperties {
  private String user;
  private String password;

  // getters & setters
}
如果您想将多个用户提供的服务映射到一个对象中,您可以为该用例使用内部类

@Configuration
public class UserProvidedServicesProperties {

  @Autowired
  private UserProvidedServiceOneProperties userProvidedService1;

  @Autowired
  private UserProvidedServiceTwoProperties userProvidedService2;

  // getters & setters

  @Configuration
  @ConfigurationProperties("vcap.services.ups-example1.credentials")
  public static class UserProvidedServiceOneProperties {
    private String user;
    private String password;

    // getters & setters
  }

  @Configuration
  @ConfigurationProperties("vcap.services.ups-example2.credentials")
  public static class UserProvidedServiceTwoProperties {
    private String user;
    private String secret;
    private String url;

    // getters & setters
  }
}