简体   繁体   English

Micronaut依赖项注入无法与picocli功能一起使用

[英]Micronaut dependency inject not working with picocli feature

I created a micronaut project using below command 我使用以下命令创建了一个micronaut项目

mn create-cli-app micronaut-cli

With in the project I created a @Singleton class and I am injecting it into my Command class but inject is not working because object is always null. 在项目中,我创建了一个@Singleton类,并将其注入到Command类中,但由于object始终为null,所以inject无法正常工作。

My Singleton class: 我的Singleton课程:

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

My Command class: 我的Command课:

@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);
        }
    }
}

When I run this class I am getting: 当我上这节课时,我得到:

converterService :null

Please help me why dependency injection is not working. 请帮助我,为什么依赖项注入不起作用。

I cannot reproduce the behavior you described. 我无法重现您描述的行为。

See the project at https://github.com/jeffbrown/cliinjection . 请参阅https://github.com/jeffbrown/cliinjection上的项目。 I pasted your classes into that project: 我将您的课程粘贴到该项目中:

https://github.com/jeffbrown/cliinjection/blob/87f289f1bb0e46a487b59665743b5bc767efa620/src/main/java/cliinjection/ConverterService.java 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 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);
        }
    }
}

That appears to work: 看来可行:

~ $ 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

EDIT 编辑

It occurs to me that one way to introduce the behavior you reported is if the project was not compiled with our annotation processors enabled. 在我看来,一种介绍您报告的行为的方法是,如果未在启用注释处理器的情况下编译项目。 The most common way for that to happen is if you are running the project directly from an IDE in a context where annotation processors are not enabled. 发生这种情况的最常见方式是,如果您在未启用注释处理器的上下文中直接从IDE运行项目。 We cover this in the user guide at https://docs.micronaut.io/1.0.5/guide/index.html#ideSetup . 我们在https://docs.micronaut.io/1.0.5/guide/index.html#ideSetup用户指南中对此进行了介绍。

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

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