简体   繁体   English

有没有办法从单个类创建多个@Client bean

[英]Is there a way to create multiple @Client beans from a single class

I am trying to use a single Kotlin class to define multiple micronuat Client beans each with its own base url.我正在尝试使用单个 Kotlin 类来定义多个 micronuat 客户端 bean,每个 bean 都有自己的基本 url。 I have a list of urls that correspond to several instances of the same webapp each of which have their own set of data, but all use the same rest endpoints.我有一个 url 列表,它们对应于同一个 webapp 的几个实例,每个实例都有自己的数据集,但都使用相同的其余端点。 So I want to be able to spin up a bunch of client beans using the same base class, but each with its own name/url.所以我希望能够使用相同的基类启动一堆客户端 bean,但每个都有自己的名称/url。 Then in a service class I would be able to use the name to pull the bean from the application context.然后在服务类中,我将能够使用该名称从应用程序上下文中提取 bean。

The work around I have come up is below.我提出的解决方法如下。 In which you define a single abstractClient class, and then extend it many times with different client names.在其中定义一个 abstractClient 类,然后使用不同的客户端名称对其进行多次扩展。 The problem with this is that I statically have to define all the possible names at compile time.这样做的问题是我必须在编译时静态地定义所有可能的名称。 I would like to be able to define some kind of map of config data in the yml of Name:URL pairs and have the clients auto generated我希望能够在 Name:URL 对的 yml 中定义某种配置数据映射,并自动生成客户端

abstract class abstractClient (
  val name: String
){
  @Get(value = "/getStuff")
  abstract suspend fun getStuff()

@Client("client1")
abstract class Client1: abstractClient("name1")

@Client("client2")
abstract class Client2: abstractClient("name2")

I can then access the beans in my service class by injecting a List and looping through them looking for the right name然后,我可以通过注入一个 List 并遍历它们以查找正确的名称来访问我的服务类中的 bean

Is there a way to create multiple @Client beans from a single class有没有办法从单个类创建多个@Client bean

Not with a single class, no.不是一个班级,不。

If your same application deployed on multiple instances then your application resource path remains same for both the servers but only your application domain and port are different.如果您的同一个应用程序部署在多个实例上,那么您的应用程序资源路径对于两台服务器都保持相同,但只有您的应用程序域和端口不同。 So in java it can be achieved by writing an interface having all resource client path/method and create separate classes which implements your common interface with different host URLs.因此,在 Java 中,可以通过编写一个具有所有资源客户端路径/方法的接口并创建单独的类来实现,这些类使用不同的主机 URL 实现您的公共接口。

public interface Base{
  @Get(value = "/getStuff")
  public void getStuff();
}


@Client("client-1 url") // define client-1 url in yml
public class Client1 implements Base{}


@Client("client-2 url") // define client-2 url in yml
public class Client2 implements Base{}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建具有不同属性的相同类的多个bean的最简单方法 - cleanest way to create multiple beans of the same class with different properties Spring:如何从单个模板定义动态创建多个bean - Spring: how to dynamically create multiple beans from single template definition 从 @Configuration class 中的一个方法创建多个 bean - Create multiple beans from one method in @Configuration class 单个JavaConfig类用于具有依赖项的相同类型的多个bean - Single JavaConfig class for multiple beans of same type with dependencies Java Guice-类似于Spring中创建多个“ bean”的guice等效方法? - Java Guice - guice equivalent way to create multiple 'beans' like in Spring? 如何在不使用 XML 文件的情况下为 class 创建多个从属 bean? - How to Create multiple Dependent beans for a class Without using XML File? 是否可以通过Java中的单个类声明创建多个对象? - Is it possible to create multiple objects from a single declaration of class in Java? 是否可以创建多个同名的 spring bean(来自不同的接口)? - is it possible to create multiple spring beans (from different interfaces) with the same name? 动态创建多个CronTriggerBean bean - Dynamically create multiple CronTriggerBean beans 如何创建 PubSubTemplate 的多个 beans - How to create multiple beans of PubSubTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM