简体   繁体   English

在 JakartaEE / Helidon / Microprofile 中手动实例化类中使用 ConfigProperty

[英]Using ConfigProperty in manually instantiated classes in JakartaEE / Helidon / Microprofile

I have a small application in Helidon start.我在 Helidon start 中有一个小应用程序。 It is mostly a REST interface, but I also want to start some background monitoring / logging on startup.它主要是一个 REST 接口,但我也想在启动时启动一些后台监控/日志记录。

I would like that monitoring to be activated / deactivated by config.我希望通过配置激活/停用该监控。 The issue I am facing is that the config is not being picked up if my class is instantiated manually.我面临的问题是,如果我的类是手动实例化的,则不会选择配置。

Here is a very short code snippet :这是一个非常短的代码片段:

Starting the application启动应用程序

public class Main {

    private Main() { }

    public static void main(final String[] args) throws IOException {
        Server server = startServer();

        CellarMonitoring monitoring = new CellarMonitoring();
        monitoring.start();
    }

    static Server startServer() {
        return Server.create().start();
    }
}

Starting monitoring or not based on Configuration :根据配置是否启动监控:

package nl.lengrand.cellar;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

public class CellarMonitoring {

    @Inject
    @ConfigProperty(name = "monitoring.enabled", defaultValue = "true")
    private volatile boolean monitoringEnabled; <= Always false

    public void start(){
        if(monitoringEnabled) {
            System.out.println("Monitoring enabled by config. Starting up");
        }
        else System.out.println("Monitoring disabled by config");
    }
}

This code will always return "Monitoring disabled by config", whatever I do.无论我做什么,此代码将始终返回“配置禁用监控”。

Accessing the config directly like described in the documentation is not really an option either since the onStartup method will never be fired.像文档中描述的那样直接访问配置也不是真正的选项,因为永远不会触发onStartup方法。

What is the proper way to inject a class in my server so it can access the config as desired?在我的服务器中注入一个类以便它可以根据需要访问配置的正确方法是什么?

Your question is actually about CDI.您的问题实际上是关于 CDI。

In order for any kind of dependency injection to work with CDI, CDI must instantiate the thing to be injected.为了让任何类型的依赖注入与 CDI 一起工作,CDI 必须实例化要注入的事物。 In this case, you instantiate the thing to be injected, so CDI never "sees" it, so it is never injected.在这种情况下,实例化要注入的事物,因此 CDI 永远不会“看到”它,因此它永远不会被注入。

I am speculating here, but I'm guessing your use case is really just: "I'd like my CellarMonitoring component to be notified when CDI comes up. How do I do that?"我在这里推测,但我猜你的用例真的只是:“我希望在 CDI 出现时通知我的CellarMonitoring组件。我该怎么做?”

There are many answers to that question on this site and elsewhere.在这个网站和其他地方有很多关于这个问题的答案。 Essentially you take advantage of the fact that CDI will fire an event notifying any interested listeners in the initialization of the application scope.本质上,您利用了这样一个事实,即 CDI 将在应用程序范围的初始化中触发一个事件,通知任何感兴趣的侦听器。 The application scope is effectively the lifespan of the application itself, so you can think of it as a startup event.应用程序范围实际上是应用程序本身的生命周期,因此您可以将其视为启动事件。

A full CDI tutorial is beyond the scope of this question and answer, but, to cut to the chase, here's a way to do it. 完整的 CDI 教程超出了这个问题和答案的范围,但是,为了切入正题,这里有一种方法可以做到。 I have had to make various assumptions, such as that you want CellarMonitoring to be singleton-like:我不得不做出各种假设,例如您希望CellarMonitoring像单例一样:

@ApplicationScoped
public class CellarMonitoring {

  @Inject
  @ConfigProperty(name = "monitoring.enabled", defaultValue = "true")
  private volatile boolean monitoringEnabled; // <= Always false

  public void start() {
    if (monitoringEnabled) {
      System.out.println("Monitoring enabled by config. Starting up");
    } else {
      System.out.println("Monitoring disabled by config");
    }
  }

  private void onStartup(@Observes @Initialized(ApplicationScoped.class) final Object event) {
    // The container has started.  You can now do what you want to do.
    this.start();
  }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM