如何在micronaut中注入嵌套(内部)非静态类?

kq0g1dla  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(173)

micronaut版本:2.2.1
java版本:11
操作系统:windows 10
我的代码如下:

import io.micronaut.context.annotation.Prototype;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class ParentObject {
    @Inject ChildClass childClass;
    String keyVar = 'FOO';

    public void parentFoo() {
        System.out.println("I am foo in parent class");
        childClass.childFoo();
    }

    @Prototype
    class ChildClass  {
        public ChildClass() {}

        public void childFoo() {
            System.out.println("I am foo in child class. keyVar is"+ ParentObject.this.keyVar);
        }
    }
}

编译过程正常。但在执行代码时,会发生以下错误:

Message: com.jcarxmate.test.ParentObject$ChildClass: method <init>()V not found
Path Taken: CoreCommand.testController --> TestController.parentObject --> ParentObject.childClass
    at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5201)
    at picocli.CommandLine$Model$CommandUserObject.getInstance(CommandLine.java:11160)
    at picocli.CommandLine$Model$CommandSpec.userObject(CommandLine.java:5928)
    at picocli.CommandLine$Interpreter.clear(CommandLine.java:12323)
    at picocli.CommandLine$Interpreter.parse(CommandLine.java:12364)
    at picocli.CommandLine$Interpreter.parse(CommandLine.java:12259)
    at picocli.CommandLine.parseArgs(CommandLine.java:1458)
    at picocli.CommandLine.execute(CommandLine.java:2057)
    at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:137)
    at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:114)
    at com.jcarxmate.CoreCommand.main(CoreCommand.java:29)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [com.jcarxmate.test.ParentObject]

Message: com.jcarxmate.test.ParentObject$ChildClass: method <init>()V not found
Path Taken: CoreCommand.testController --> TestController.parentObject --> ParentObject.childClass
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1925)
    at io.micronaut.context.DefaultBeanContext.getScopedBeanForDefinition(DefaultBeanContext.java:2424)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2339)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2311)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1245)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1441)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1203)
    at com.jcarxmate.test.$ParentObjectDefinition.injectBean(Unknown Source)
    at com.jcarxmate.test.$ParentObjectDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1898)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingletonInternal(DefaultBeanContext.java:2679)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2665)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2337)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2311)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1245)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1441)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1203)
    at com.jcarxmate.test.$TestControllerDefinition.injectBean(Unknown Source)
    at com.jcarxmate.test.$TestControllerDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1898)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingletonInternal(DefaultBeanContext.java:2679)
    at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2665)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2337)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2311)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1245)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1441)
    at io.micronaut.context.AbstractBeanDefinition.getBeanForField(AbstractBeanDefinition.java:1203)
    at com.jcarxmate.$CoreCommandDefinition.injectBean(Unknown Source)
    at com.jcarxmate.$CoreCommandDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1898)
    at io.micronaut.context.DefaultBeanContext.getScopedBeanForDefinition(DefaultBeanContext.java:2424)
    at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:2339)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:2311)
    at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:1266)
    at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:744)
    at io.micronaut.context.BeanLocator.findOrInstantiateBean(BeanLocator.java:163)
    at io.micronaut.configuration.picocli.MicronautFactory.create(MicronautFactory.java:73)
    at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5197)
    ... 10 more
Caused by: java.lang.NoSuchMethodError: com.jcarxmate.test.ParentObject$ChildClass: method <init>()V not found
    at com.jcarxmate.test.$ParentObject$ChildClassDefinition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1898)
    ... 47 more

如果我移除 @Inject 从线路上 @Inject ChildClass childClass; 并添加 childClass = new ChildClass(); 其工作原理如下:

public void parentFoo() {
    System.out.println("I am foo in parent class");
    childClass = new ChildClass();
    childClass.childFoo();
}

但如果可能的话,我还是希望使用@inject。但是,我不能使用嵌套的静态类,因为 ChildClass 必须访问父类中的变量和方法。
有什么建议吗?谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题