gpt4 book ai didi

java - Micronaut 依赖注入(inject)不适用于 picocli 功能

转载 作者:行者123 更新时间:2023-12-01 17:44:39 25 4
gpt4 key购买 nike

我使用以下命令创建了一个 micronaut 项目

mn create-cli-app micronaut-cli

在项目中,我创建了一个 @Singleton 类,并将其注入(inject)到我的 Command 类中,但注入(inject)不起作用,因为对象始终为 null。

我的单例类:

@Singleton
public class ConverterService {
public String service(){return "good service";}
}

我的命令类:

@Command(name = "mini-java-util", description = "...",
mixinStandardHelpOptions = true)
public class MiniJavaUtilCommand implements Runnable {

@Inject ConverterService converterService;
@Option(names = {"-v", "--verbose"}, description = "...")
boolean verbose;

public static void main(String[] args) throws Exception {
PicocliRunner.run(MiniJavaUtilCommand.class, args);
}

public void run() {
// business logic here
if (verbose) {
System.out.println("converterService :" + converterService);
}
}
}

当我运行这门课时,我得到:

converterService :null

请帮助我解释为什么依赖注入(inject)不起作用。

最佳答案

我无法重现您所描述的行为。

查看该项目 https://github.com/jeffbrown/cliinjection 。我将您的类(class)粘贴到该项目中:

https://github.com/jeffbrown/cliinjection/blob/87f289f1bb0e46a487b59665743b5bc767efa620/src/main/java/cliinjection/ConverterService.java

package cliinjection;

import javax.inject.Singleton;

@Singleton
public class ConverterService {
public String service(){return "good service";}
}

https://github.com/jeffbrown/cliinjection/blob/87f289f1bb0e46a487b59665743b5bc767efa620/src/main/java/cliinjection/MiniJavaUtilCommand.java

package cliinjection;

import io.micronaut.configuration.picocli.PicocliRunner;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import javax.inject.Inject;

@Command(name = "mini-java-util", description = "...",
mixinStandardHelpOptions = true)
public class MiniJavaUtilCommand implements Runnable {

@Inject
ConverterService converterService;
@Option(names = {"-v", "--verbose"}, description = "...")
boolean verbose;

public static void main(String[] args) throws Exception {
PicocliRunner.run(MiniJavaUtilCommand.class, args);
}

public void run() {
// business logic here
if (verbose) {
System.out.println("converterService :" + converterService);
}
}
}

这似乎有效:

~ $ git clone git@github.com:jeffbrown/cliinjection.git
Cloning into 'cliinjection'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 21 (delta 0), reused 21 (delta 0), pack-reused 0
Receiving objects: 100% (21/21), 53.36 KiB | 1.16 MiB/s, done.
~ $
~ $ cd cliinjection/
cliinjection master $
cliinjection master $ ./gradlew assemble

> Task :compileJava
Note: Creating bean classes for 2 type elements

BUILD SUCCESSFUL in 3s
10 actionable tasks: 10 executed
cliinjection master $
cliinjection master $ java -jar build/libs/cliinjection-0.1-all.jar -v
07:35:30.515 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [cli]
converterService :cliinjection.ConverterService@7fd4acee

编辑

我发现引入您报告的行为的一种方法是,如果项目不是在启用我们的注释处理器的情况下编译的。发生这种情况的最常见方法是,如果您在未启用注释处理器的上下文中直接从 IDE 运行项目。我们在用户指南中介绍了这一点:https://docs.micronaut.io/1.0.5/guide/index.html#ideSetup .

关于java - Micronaut 依赖注入(inject)不适用于 picocli 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128012/

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