当前位置: 首页 > 知识库问答 >
问题:

Micronaut-创建、获取和销毁Application ationContext bean

谢典
2023-03-14

我需要动态创建和销毁 HttpClient 对象,以对应使用我的 Micronaut 应用程序注册/注销自己的客户端。作为其中的一部分,我想将它们作为 bean 添加到应用程序上下文中,以便它们在项目中也自动使用自定义 HttpFilters。

我认为使用名为QualifierApplicationContextbean方法来管理这些bean会非常简单,但这些API的行为方式让我感到困惑:

  1. applicationContext.createBean(HttpClient.class,Qualifiers.byName("myLabel"), myUrl)失败:
io.micronaut.context.exceptions.NoSuchBeanException: No bean of type 
[io.micronaut.http.client.HttpClient] exists. Make sure the bean is not disabled by bean 
requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the 
bean is enabled then ensure the class is declared a bean and annotation processing is 
enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as 
an annotation processor).

为什么bean是否存在很重要?我在努力创造它!

io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [io.micronaut.http.client.DefaultHttpClient]

Message: Missing bean argument [LoadBalancer loadBalancer] for type: io.micronaut.http.client.DefaultHttpClient. Required arguments: LoadBalancer loadBalancer,HttpClientConfiguration configuration,String contextPath,HttpClientFilterResolver filterResolver

为什么它试图创造它?我只想找到它!

(注。< code > application context . get bean(http client . class,qualifiers . byname(" my label "))可能在这里工作,但是因为我还不能解决第一点,所以我还不能验证这一点)

我假设我在这里使用了错误的API,但正确的API是什么?

总而言之,我完全糊涂了——任何关于正确使用这些API的帮助都是受欢迎的。

共有1个答案

古棋
2023-03-14

HttpClient 是用于定义 http 客户端而不是 bean 的接口(请注意,Micronaut 提供的默认 ~ DefaultHttpClient ~ 实现 不是 bean)。因此,对上述 ApplicationContext 上下文查找或实例化方法的任何调用都会导致错误:

  • ApplicationContext#createBean
  • ApplicationContext#findBean
  • ApplicationContext#getBean
  • ... // 其他实例化方法

注意这是有意设计的,因为< code>HttpClient API旨在通过AOP通知自动注入和运行。

您可以注入原始(使用默认方法)HttpClient

@Singleton
public class MyBeanIml implements MyBean {

  @Client
  HttpClient client;

  // ...
}

或者对于自定义方法、生成的实现,可以使用<code>HttpClient</code>声明性实现:

@Client("endpoint-uri")
public interface MyHttpClient {

  @Get
  String query();

  // ...
}

如果不指定创建自定义(顺便说一下,动态在此上下文中太模糊)HttpClient 的确切要求,您可以使用自定义 @Prototyped bean 包装注入的 HttpClient,该 HttpClient 可以使用您的自定义参数进行实例化,并将方法委托给包装的 HttpClient或实现您的自定义方法:

@Prototype
public class CustomHttpClientImpl implements CustomHttpClientImpl.CustomHttpClient {

    @Inject
    @Client
    private final HttpClient httpClient;

    private final String endpoint;

    public CustomHttpClientImpl(@Parameter String endpoint, HttpClient httpClient) {
        this.httpClient = httpClient;
        this.endpoint = endpoint;
    }

        @Override
    public String query() {
        return this.httpClient.toBlocking()
                .exchange(HttpRequest.GET(URI.create(this.endpoint)), String.class)
                .body();
    }
    
    // delegate to the `httpClient` bean or implement your custom API querying methods

    public interface CustomHttpClient {

        String query();
    }
}

然后,您可以使用Application ationContext创建原型d bean实例:

applicationContext.createBean(CustomHttpClient.class, "som-uri-for-custom-client");
 类似资料:
  • 创建新节点 除了通过场景编辑器创建节点外,我们也可以在脚本中动态创建节点。通过 new cc.Node() 并将它加入到场景中,可以实现整个创建过程。 以下是一个简单的例子: cc.Class({ extends: cc.Component, properties: { sprite: { default: null, type: cc.SpriteFra

  • 创建新节点 除了通过场景编辑器创建节点外,我们也可以在脚本中动态创建节点。通过 new Node() 并将它加入到场景中,可以实现整个创建过程。 以下是一个简单的例子: import { _decorator, Component, Node } from "cc"; const { ccclass, property } = _decorator; @ccclass("test") expor

  • 在 3D 场景初始化后,我们可以通过 create() 方法来创建物体,例如,加载模型、添加标记、创建基本形体等。我们还可以通过 create() 方法来加载园区,例如,添加多个园区。通过 destroy() 方法,可以销毁物体,即在场景中删除物体。 创建物体的语法 下面的代码中,通过 create() 方法创建物体,并通过对象名称和属性,来添加所创建的物体属性。 var obj = app.cr

  • 创建和销毁游戏对象 某些游戏在场景中维护恒定数量的对象,但是在游戏过程中,创建和移除人物、物品以及其他对象也非常普遍。在 Unity 中,一个游戏对象可以通过 Instantiate 函数创建一个已有对象的新副本。 public GameObject enemy; void Start() { for (int i = 0; i < 5; i++) { Instantiat

  • 问题内容: 如何在WordPress中设置,获取和销毁cookie? 我在网上冲浪,但思路不清楚,请帮助我找到方法。 问题答案: 您可以使用PHP在服务器端或客户端使用JavaScript在服务器端检索和操作Cookie。 在PHP中,您可以使用设置Cookie 。请注意,必须在将任何输出发送到浏览器之前完成此操作,这在Wordpress中可能是很大的挑战。您几乎只限于可以通过插件或主题文件(例如

  • 我的流在数据库中配置,我的程序不断创建和销毁流。 因此,流配置(例如cron配置)可以随时更改。 这些流是用方法IntegrationFlowContext注册的。使用IntegrationFlowRegistration方法注册并销毁。销毁。 流的运行从第0秒开始,可以在任何一分钟开始。销毁和创建新流从每分钟1秒开始。 这是一个好方法吗?当我测试这个时,它起作用了。但我在想,这是一种很好的方法吗