简体   繁体   English

有没有一种方法可以注入其构造函数在micronaut中具有参数的类?

[英]Is there a way you can inject a class whose constructor has parameters in micronaut?

Suppose I have class A. 假设我有A班。

class A {
    private String s;

    public A(String s) {
        this.s = s;
    }
}

and I want to inject A into class B. 我想将A注入B类。

So far, I have been injecting classes without constructor parameters like 到目前为止,我一直在注入没有构造函数参数的类,例如

class B {

    @Inject
    private A a;
}

But I don't know how classes with constructor parameters can be injected. 但是我不知道如何注入带有构造函数参数的类。 How can I do this? 我怎样才能做到这一点?

You need to prepend your constructor with @Inject to make it injectable. 您需要在构造函数之前添加@Inject使其可注入。 Please check https://docs.oracle.com/javaee/6/api/javax/inject/Inject.html for documentation and examples. 请检查https://docs.oracle.com/javaee/6/api/javax/inject/Inject.html以获取文档和示例。

Instead of this: 代替这个:

@Singleton
class B {
    @Inject
    private A a;
}

You could have this: 你可能有这个:

@Singleton
class B {

    private A a;

    public B(A a) {
        this.a = a;
    }
}

I think in your example A would be a prototype bean because it has an argument that you must provide. 我认为在您的示例A中将是原型bean,因为它具有必须提供的参数。 Because you must provide the argument you can't simply inject the bean like you would a singleton. 因为必须提供参数,所以不能像单例那样简单地注入bean。 You can inject the BeanContext and call createBean . 您可以注入BeanContext并调用createBean For example: beanContext.createBean(A.class, "some string value") . 例如: beanContext.createBean(A.class, "some string value") That would require A be annotated with @Prototype and any constructor parameters that should be provided by the creator with @Parameter . 这将要求A带有@Prototype注释,并且创建者应使用@Parameter对其提供的任何构造函数参数进行注释。

暂无
暂无

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

相关问题 如何将模拟注入具有 1 个带参数的构造函数的类? - How to inject mocks into a class that has 1 constructor with parameters? 为什么不能从构造函数是私有的类继承? - Why can you not inherit from a class whose constructor is private? 应用程序如何通知Spring应用程序上下文在哪里找到用@Inject注释其构造函数的类? - How can an application inform the Spring application context where to find a class whose constructor is annotated with @Inject? Guice:使用带有自己参数的构造函数注入 class? - Guice: Inject class with constructor with own parameters? 你能在构造函数方法参数中设置类变量吗? - Can you set class variables in the constructor method parameters? 实例化类,该类的无参数构造函数不可用并且参数未知 - Instantiating class whose no parameter constructor is not available and parameters are not known 注入具有带有通用类参数的构造函数的通用bean的方法 - Way to inject generic bean that have constructor with generic class argument 在java中,如何使用私有构造函数创建一个类,其超类也有一个私有构造函数? - In java, how do I make a class with a private constructor whose superclass also has a private constructor? 有没有一种方法可以调用一个父类构造函数,该构造函数从一个不具有自己构造函数的子类中获取参数? - Is there a way I can call a parent class constructor that takes parameters from a child class that does not have a constructor of it's own? 有没有办法注入一个依赖项,以便它可以在构造函数中使用? - Is there any way to inject a dependency so that it can be used in the constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM