Java 将不同的@Configuration bean传递给基于rest的客户端
我有两个不同的配置,我将从应用程序yml加载。属性相同,但值可能不同 如何使此工作giveMeRestTemplate(类型配置) //app.ymlJava 将不同的@Configuration bean传递给基于rest的客户端,java,spring,spring-boot,autowired,resttemplate,Java,Spring,Spring Boot,Autowired,Resttemplate,我有两个不同的配置,我将从应用程序yml加载。属性相同,但值可能不同 如何使此工作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 所以我有不同的Conf
bus:
tyres:8
seats:40
color:red
url: www.businfo.com
car:
tyres:4
seats:6
color:blue
url: www.carinfo.com
所以我有不同的ConfigruationProperties类,比如下面的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可以返回不同类型车辆的信息
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模板
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
}
}
我建议您稍微更改一下配置属性(为了简洁起见,省略了getter和setter): 最后是你的助手:
@Service
public class RestClientService {
@Autowired
private RestConfig config;
public RestTemplate giveMeRestTemplate(Type type) {
RestConfig.ConfigType cfg = config.getType().get(type);
// do what's necessary with cfg
return restTemplate;
}
}
无论@Archie(谢谢)发布了什么,都让我有了这样的洞察力
public enum Type {
BUS, CAR
}
所以将字符串存储为map中的键,这会告诉我该配置是哪种特定类型
@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;
}
}
客户端代码
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
}
}
您的问题是什么?giveMeRestTemplate(Type Type)如何使其工作其他人可以帮助使其通用?您可以帮助了解RestConfig映射中的内容吗?您的
application.yml
文件的属性由Spring映射到那里。您也可以在文章中看到示例配置文件。因此,这里我再次传递包含总线和汽车配置的整个类型enum,但是为什么我的rest模板需要它呢?如果我给它发送一个确切的配置类型,要么是总线配置,要么是汽车配置。所以,我们的目标是避免在RestClientService中编写逻辑,在这里我们编写逻辑是为了获取类型,或者知道这是哪种配置RestConfig.ConfigType cfg=config.getType().get(type);'如何保持这个泛型,让它只给我rest模板和任何属性,我通过参数发送它,比如我们pascontext
到方法,你只传递一个类型枚举。从映射中检索值几乎不是什么逻辑。在您的示例中,CarConfig和BusConfig是独立的类,不能同时被单个方法接受——除非该方法只接受对象,但这离通用解决方案越远越好。如果需要任意参数,可以尝试传递Map。“我传递包含总线和汽车配置的整个类型枚举”-情况并非如此。类型枚举不包含任何内容,它是包含不同类型的配置的配置属性。
@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;
}
}
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
}
}