将不同的@Configuration bean传递给基于rest的客户端 [英] pass different @Configuration bean to a rest based Client

查看:87
本文介绍了将不同的@Configuration bean传递给基于rest的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种不同的配置,可以从应用程序yml加载.属性相同,但值可能不同.

I have two different configuration which i would be loading from application yml. The properties are same but the values might differ.

我如何进行这项工作 giveMeRestTemplate(类型配置)

//app.yml

    bus:
      tyres:8
      seats:40
      color:red
      url: www.businfo.com
    car:
      tyres:4
      seats:6
      color:blue
      url: www.carinfo.com

因此,我为此有不同的ConfigruationProperties类,例如CarConfig

So i have different ConfigruationProperties class for this like below one other as CarConfig

@ConfigurationProperties("bus")
    public class BusConfig{
      public int tyres;
      public int seats;
      public string color ;
      public string url;
    //setters and getters below.

    }

然后我有一个rest客户,我可以使用它调用一些api来获取信息.因此,此api可以返回您可以说的不同类型车辆的信息.

Then i have a rest client which i use to invoke some api to fetch information. So this api can return information of different types of vehicles you can say.

public class RestClientHelper{

    public RestTemplate giveMeRestTemplate(Type config);
    {
     return restTemplate; //using the above type which might have url to the specific api to call.

     }
}

这个想法是,调用代码可以根据发送给它的配置获取不同的rest模板.

The idea is that the calling code can get different rest templates based on what config was sent to it.

   public SomeClient{

    @Autowired
    RestClientHelper rch;
    
    @Autowired
    BusConfig bc;

    @Autowired
    CarConfig cc;

    
    public void publishDetails(){
     rch.giveMeRestTemplate(bc);    //so if i send cc then it should prepare rest template for cc
    }
   }

推荐答案

@Archie发布的所有内容(谢谢)此处给了我洞察力,可以这样写.

Whatever posted by @Archie(Thanks) here gave me insight to write it like this.

public enum Type {
    BUS, CAR
}

因此将字符串作为键存储在map中,这会告诉我此配置属于哪种特定类型.

So storing the string as key in map,which tell me to which specific type this config is.

   @ConfigurationProperties("rest-config")
    public class RestConfig {
        private Map<String, ConfigType> type = new HashMap<>();
        public static class ConfigType {
            private int tyres;
            private int seats;
            private string color;
            private string url;
        }
    }

因此,帮助程序可以采用类型实际的配置类型(这是我的调用方代码可以根据它可以创建模板的类型向其发送类型的信息,而不是在此方法内部读取有关它是哪种配置类型的信息.)

So helper can take a type actual config type(this is where my caller code can send it a type based on which template can be created rather than reading inside this method about which type of config it is.)

    public class RestClientHelper{
    
        public RestTemplate giveMeRestTemplate(RestConfig.type config);
        {
         return restTemplate; //using the above type which might have url to the specific api to call.
    
         }
    }

客户代码

public SomeClient{

    @Autowired
    RestClientHelper rch;
    
    public void publishDetails(){
     rch.giveMeRestTemplate(rch.type.get(Type.BUS.toString()));    //I am sending a actual type by using enum to match the string name
    }
   }

这篇关于将不同的@Configuration bean传递给基于rest的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆