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

mn create-cli-app micronaut-cli


在项目中,我创建了一个@Singleton类,并将其注入到Command类中,但由于object始终为null,所以注入无法正常工作。

我的Singleton课程:

@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


请帮助我,为什么依赖项注入不起作用。

最佳答案

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

请参见https://github.com/jeffbrown/cliinjection上的项目。我将您的课程粘贴到该项目中:

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 [email protected]: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用户指南中对此进行了介绍。

10-06 09:37