Reputation: 419
I created a micronaut project using below command
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.
My Singleton class:
@Singleton
public class ConverterService {
public String service(){return "good service";}
}
My Command class:
@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.
Upvotes: 1
Views: 2718
Reputation: 27200
I cannot reproduce the behavior you described.
See the project at https://github.com/jeffbrown/cliinjection. I pasted your classes into that project:
package cliinjection;
import javax.inject.Singleton;
@Singleton
public class ConverterService {
public String service(){return "good service";}
}
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 [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
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. We cover this in the user guide at https://docs.micronaut.io/1.0.5/guide/index.html#ideSetup.
Upvotes: 6