【发布时间】:2018-07-13 02:38:49
【问题描述】:
我有一个包含两个实现的接口,现在我必须添加更多实现。每个实现接口在构造函数中都有不同的参数,自动连接,bean 被注入。 我需要编写一个通用的工厂方法,如果我传入一个客户端,它会返回特定类型的实现,并设置所有自动连接的属性。请指教怎么办?
Interface Example{
List<String> getData(String url);
boolean isCorrectData(String var1, String var2);
}
客户端1:
public class client1 implements Example{
final XclientConfig config;
final RestTemplate template;
@Autowired
public client1(XClientConfig config, @Qualifier(“template1”) RestTemplate template){
this.config = config;
this.template = template;
}
客户端 2:
public class client2 implements Example{
final YclientConfig config;
final RestTemplate template;
@Autowired
public client2(YClientConfig config, @Qualifier(“template2”) RestTemplate template){
this.config = config;
this.template = template;
}
现在我正在尝试编写一个工厂类,在其中传入客户端名称(客户端 1 或客户端 2),该工厂将返回一个完整构造的 Example 实例,因此不应该有任何未实现的依赖项。 我需要通过工厂方法注入所有依赖项。 能给个建议吗?
【问题讨论】:
-
您能否更具体地了解您的具体用例? 通常有更简洁的方法可以做到这一点。例如,您真的需要多个
RestTemplate选项吗?如果是这样,它们有何不同? -
我需要创建一个工厂类,它根据我传递的客户端字符串返回特定类型的客户端
-
这没有进一步解释。
标签: java spring spring-mvc spring-boot