Quarkus:将配置属性注入服务 class 不起作用

Quarkus: Inject config properties into service class doesn't work

我在将配置 class 注入 Quarkus 中的服务 class 时遇到了一些问题。 我有这个配置 class:

import io.quarkus.arc.config.ConfigProperties;

@ConfigProperties(prefix = "database")
public class TestConfig {
    public String url = "http://localhost:8086";
    public String user;
    public String password;
    public String name;
    public ColumnConfig column;
    public int pagination = 1000;


    public static class ColumnConfig {
        public String value = "value";
        public String device;
        public String type;
    }
}

当我使用它时我的资源class没问题

@Path("/measurements")
@Tag(name = "measurements")
@Produces(MediaType.APPLICATION_JSON)
public class MeasurementResource {

    @Inject
    MeasurementService delegate;

    @Inject
    TestConfig config;

    @GET
    @Path("/last")
    public MeasurementResult getLastMeasurements() {
        System.out.println(config.url); //works
        //...

    }

但我实际上想使用 MeasurementService class。但它在那里不起作用。

@ApplicationScoped
public class MeasurementService {
    
    private static InfluxDBMapper mapper;
    
    @Inject
    TestConfig config;

    public MeasurementService() {
        System.out.println(config.url);     // exception here
        System.out.println(config.password);
        System.out.println(config.column.tenant);
        config = DatabaseConfig.getInstance();
        InfluxDB influxDB = InfluxDBFactory.connect(config.url, config.user, config.password);
        //...

我收到一个异常,指出由于 NullPointerException 而无法注入 MeasurementService。执行打印命令时发生异常。我在上面的代码片段中标记了它。

org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Error injecting de.*.services.MeasurementService de.*.resources.MeasurementResource.delegate
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:381)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke(SynchronousDispatcher.java:261)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess[=14=](SynchronousDispatcher.java:161)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)
    at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)
    at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:136)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access[=14=]0(VertxRequestHandler.java:40)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.run(VertxRequestHandler.java:97)
    at io.quarkus.runtime.CleanableExecutor$CleaningRunnable.run(CleanableExecutor.java:231)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at java.base/java.lang.Thread.run(Thread.java:834)
    at org.jboss.threads.JBossThread.run(JBossThread.java:479)
Caused by: java.lang.RuntimeException: Error injecting de.*.services.MeasurementService de.*.resources.MeasurementResource.delegate
    at de.*.resources.MeasurementResource_Bean.create(MeasurementResource_Bean.zig:171)
    at de.*.resources.MeasurementResource_Bean.create(MeasurementResource_Bean.zig:194)
    at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
    at io.quarkus.arc.impl.AbstractSharedContext.access[=14=]0(AbstractSharedContext.java:14)
    at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:29)
    at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
    at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
    at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
    at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
    at de.*.resources.MeasurementResource_Bean.get(MeasurementResource_Bean.zig:226)
    at de.*.resources.MeasurementResource_Bean.get(MeasurementResource_Bean.zig:242)
    at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:435)
    at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:448)
    at io.quarkus.arc.impl.ArcContainerImpl.get(ArcContainerImpl.java:271)
    at io.quarkus.arc.impl.ArcContainerImpl.get(ArcContainerImpl.java:268)
    at io.quarkus.arc.runtime.BeanContainerImpl.create(BeanContainerImpl.java:35)
    at io.quarkus.resteasy.common.runtime.QuarkusConstructorInjector.construct(QuarkusConstructorInjector.java:54)
    at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.createResource(POJOResourceFactory.java:71)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:386)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:68)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:492)
    ... 20 more
Caused by: java.lang.NullPointerException
    at de.*.services.MeasurementService.<init>(MeasurementService.java:43)
    at de.*.MeasurementService_ClientProxy.<init>(MeasurementService_ClientProxy.zig:24)
    at de.*.MeasurementService_Bean.proxy(MeasurementService_Bean.zig:40)
    at de.*.MeasurementService_Bean.get(MeasurementService_Bean.zig:221)
    at de.*.services.MeasurementService_Bean.get(MeasurementService_Bean.zig:237)
    at de.*.resources.MeasurementResource_Bean.create(MeasurementResource_Bean.zig:154)
    ... 40 more

我猜是有问题,因为我尝试将 class 注入到 class 中,该 class 也被注入到其他地方,但我不确定并希望有解决方案.

谢谢。

编辑:

解决方案是像这样在构造函数中注入class:

@ApplicationScoped
public class MeasurementService {
    
    private static InfluxDBMapper mapper;
    
    @Inject
    public MeasurementService(TestConfig config) {

感谢 Luca 的想法。

我不是 100% 确定,但是属性是在对象构造之后绑定的;这就是 TestConfig config 为空的原因。
改为使用构造函数注入: @Inject public MeasurementService(TestConfig config)