Micronaut - 在不创建 ApplicationContext 的情况下创建一个 bean
Micronaut - Create a bean without creating ApplicationContext
我有一个使用@client 注释编写的 Micronaut 声明式 HTTP 客户端。我想在创建 ApplicationContext itslef 之前启动 micronaut 应用程序时调用它。
HTTP 客户端:SampleHttpClient.java
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
@Client("http://127.0.0.1:8200")
@Header(name = "X-Vault-Token", value = "hvs.CEGT7cKyMA8wsDbgKZqxC34q")
public interface SampleHttpClient {
@Get(value = "/v1/kv/data/KMS", produces = MediaType.APPLICATION_JSON)
HttpResponse<String> getVaultSecret();
}
Application.java(主要class)
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
// Following code works perfect. I am creating context here. But I dont want to do this
SampleHttpClient client = Micronaut.run(Application.class, args).
getBeansOfType(SampleHttpClient.class).stream().findFirst().get();
System.out.println("Response Body ="+client.getVaultSecret().body());
// How do we get the instance of SampleHttpClient without using Micronaut's dependency
injection process???
}
}
I want to call this while starting micronaut app before creating the
ApplicationContext itslef.
Micronaut 不提供支持该功能的机制。您可以编写自己的实例来实例化 bean,但这是一项艰巨的任务。您将编写自己的 bean 容器。
我有一个使用@client 注释编写的 Micronaut 声明式 HTTP 客户端。我想在创建 ApplicationContext itslef 之前启动 micronaut 应用程序时调用它。
HTTP 客户端:SampleHttpClient.java
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
@Client("http://127.0.0.1:8200")
@Header(name = "X-Vault-Token", value = "hvs.CEGT7cKyMA8wsDbgKZqxC34q")
public interface SampleHttpClient {
@Get(value = "/v1/kv/data/KMS", produces = MediaType.APPLICATION_JSON)
HttpResponse<String> getVaultSecret();
}
Application.java(主要class)
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
// Following code works perfect. I am creating context here. But I dont want to do this
SampleHttpClient client = Micronaut.run(Application.class, args).
getBeansOfType(SampleHttpClient.class).stream().findFirst().get();
System.out.println("Response Body ="+client.getVaultSecret().body());
// How do we get the instance of SampleHttpClient without using Micronaut's dependency
injection process???
}
}
I want to call this while starting micronaut app before creating the ApplicationContext itslef.
Micronaut 不提供支持该功能的机制。您可以编写自己的实例来实例化 bean,但这是一项艰巨的任务。您将编写自己的 bean 容器。