用Spring Annotation替换<constructor-arg>

Rob*_*tis 8 java generics spring annotations code-injection

有一种方法可以用Annotation替换constructor-arg吗?

我有这个构造函数:

public GenericDAOImpl(Class<T> type) {
    this.type = type;
}
Run Code Online (Sandbox Code Playgroud)

我需要在我的Facade中注入:

@Inject
private GenericDAO<Auto, Long> autoDao;
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何在costructor中传递参数的值.

先感谢您

[更多信息]我试着解释我的问题.

<bean id="personDao" class="genericdao.impl.GenericDaoHibernateImpl">
        <constructor-arg>
            <value>genericdaotest.domain.Person</value>
        </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)

我想只使用注释转换该代码.有人可以解释一下吗?

Boz*_*zho 2

更新:恐怕不可能做你想做的事。您无法从注入点的参数中获取构造函数参数。AFactoryBean将是第一个查看的地方,但它没有给出注入点元数据。(需要注意的是:这种情况很容易被CDI覆盖)

原始答案:(如果您在外部配置类型,这可能仍然有效)

@Inject只需在构造函数上使用即可。但请注意,spring 不赞成构造函数注入。考虑设置器/字段注入。

然而,就您的情况而言,您可能有多个 类型的 bean Class。如果是这种情况,您可以使用@Resource(name="beanName").

来自以下文档javax.inject.Inject

可注入构造函数使用 @Inject 进行注释,并接受零个或多个依赖项作为参数。@Inject 最多可以应用于每个类的一个构造函数。

   @Inject ConstructorModifiersopt SimpleTypeName(FormalParameterListopt)  
   Throwsopt ConstructorBody
Run Code Online (Sandbox Code Playgroud)