gpt4 book ai didi

java - 如何从 micronaut 框架内的服务访问 ApplicationContext?

转载 作者:行者123 更新时间:2023-12-01 19:36:26 25 4
gpt4 key购买 nike

我在服务中访问 ApplicationContext 的每次尝试都失败,ApplicationContext 返回为 null。在 Spring 中,我能够使用 ApplicationContextAware,根据( https://micronaut-projects.github.io/micronaut-spring/latest/guide/index.html )应将其替换为 micronaut 中的 @Autowired 。我已经尝试过

@Autowired ApplicationContext context

但是没有运气。它始终为空。任何帮助将不胜感激!

最佳答案

该项目位于https://github.com/jeffbrown/injectcontext显示了注入(inject) ApplicationContext 的 2 种方法。

https://github.com/jeffbrown/injectcontext/blob/1f319a3bb3db8eff05f159dd8dc642b227662307/src/main/java/injectcontext/FirstService.java

package injectcontext;

import io.micronaut.context.ApplicationContext;

import javax.inject.Singleton;

@Singleton
public class FirstService {

private final ApplicationContext applicationContext;

public FirstService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

public boolean isContextNull() {
return applicationContext == null;
}
}

https://github.com/jeffbrown/injectcontext/blob/1f319a3bb3db8eff05f159dd8dc642b227662307/src/main/java/injectcontext/SecondService.java

package injectcontext;

import io.micronaut.context.ApplicationContext;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class SecondService {

@Inject
ApplicationContext applicationContext;

public boolean isContextNull() {
return applicationContext == null;
}
}

https://github.com/jeffbrown/injectcontext/blob/1f319a3bb3db8eff05f159dd8dc642b227662307/src/main/java/injectcontext/DemoController.java

package injectcontext;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/")
public class DemoController {

FirstService firstService;

SecondService secondService;

public DemoController(FirstService firstService, SecondService secondService) {
this.firstService = firstService;
this.secondService = secondService;
}

@Get("/first")
public String first() {
boolean isNull = firstService.isContextNull();

return "firstService.isContextNull() == " + isNull;
}

@Get("/second")
public String second() {
boolean isNull = secondService.isContextNull();

return "secondService.isContextNull() == " + isNull;
}
}

一切正常:

 $ curl http://localhost:8080/first
firstService.isContextNull() == false
$
$ curl http://localhost:8080/second
secondService.isContextNull() == false

编辑

如果您真正想要的只是检索服务 ID,您可以执行以下操作:

package injectcontext;

import io.micronaut.discovery.DiscoveryClient;
import org.reactivestreams.Publisher;

import javax.inject.Singleton;
import java.util.List;

@Singleton
public class DiscoveryHelper {

private final DiscoveryClient consulClient;

public DiscoveryHelper(DiscoveryClient consulClient) {
this.consulClient = consulClient;
}

public Publisher<List<String>> getIds() {
// do whatever you want with the ids...
return consulClient.getServiceIds();
}
}

如果您在启用了 consul 的服务中拥有该功能,那么这将起作用。

consul:
client:
registration:
enabled: false # set to true if you want this service to register itself
defaultZone: "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"

关于java - 如何从 micronaut 框架内的服务访问 ApplicationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57278213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com