replace with Spring Annotation

后端 未结 4 1567
傲寒
傲寒 2021-01-02 07:32

there is a way to replace constructor-arg with Annotation?

I have this constructor:

public GenericDAOImpl(Class type) {
    this.type = type         


        
4条回答
  •  自闭症患者
    2021-01-02 08:24

    Update: I'm afraid it is not possible to do what you are trying to. You can't get constructor arguments from the parameters of the injection point. A FactoryBean would be the first place to look, but it isn't given the injection point metadata. (To be noted: this case is easily covered by CDI)

    Original answer: (that may still work if you configure your types externally)

    Simply use @Inject on the constructor. But note that spring frowns upon constructor injection. Consider setter/field injection.

    In your case, however, you're likely to have more than one beans of type Class. If this is the case, you can use @Resource(name="beanName").

    From the docs of javax.inject.Inject:

    Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class.

       @Inject ConstructorModifiersopt SimpleTypeName(FormalParameterListopt)  
       Throwsopt ConstructorBody
    

提交回复
热议问题