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

How do I access the ApplicationContext from a service within the micronaut framework?

我在我的服务中访问 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 的项目显示了 2 种注入 ApplicationContext 的方法。

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}"