更换<构造带参数>与Spring注解 [英] replace <constructor-arg> with Spring Annotation

查看:259
本文介绍了更换<构造带参数>与Spring注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以替代构造 - 精氨酸与注释?

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

我有这样的构造:

public GenericDAOImpl(Class<T> type) {
    this.type = type;
}

和我需要注入,在我的门面:

and i need to inject that in my Facade:

@Inject
private GenericDAO<Auto, Long> autoDao;

问题是,我不知道如何通过在costructor参数的值。

The problem is that i don't know how to pass the value of parameter in costructor.

感谢您提前

[详细信息]
我试着解释我的问题。

[More Info] I try to explain my problem.

<bean id="personDao" class="genericdao.impl.GenericDaoHibernateImpl">
        <constructor-arg>
            <value>genericdaotest.domain.Person</value>
        </constructor-arg>
</bean>

我想用只记转换是code。
有人能解释如何?

I want convert that code using only annotation. Someone can explain how?

推荐答案

我觉得 @Inject 本身并不能帮助,你将不得不使用 @Qualifier 标注也。

I think @Inject alone won't help, you will have to use a @Qualifier annotation also.

下面是春季参考的相关部分:结果
<一href=\"http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers\"相对=nofollow> 3.9.3微调基于注解的自动连接带预选赛

Here's the relevant Section of the Spring Reference:
3.9.3 Fine-tuning annotation-based autowiring with qualifiers

如果我理解正确此,你将不得不使用 @Qualifier 机制。

If I understand this correctly, you will have to use the @Qualifier mechanism.

如果您使用<一个href=\"http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/Qualifier.html\"相对=nofollow> Spring的 @Qualifier 标注,你也许可以做到这一点的内联,是这样的:

If you use Spring's @Qualifier annotation, you can probably do it inline, something like this:

@Repository
public class DaoImpl implements Dao{

    private final Class<?> type;

    public DaoImpl(@Qualifier("type") final Class<?> type){
        this.type = type;
    }

}

但是,如果你使用 JSR-330 @Qualifier 标注,我想你将不得不创建一个标有 @Qualifier 。

另一种可能性是在<一个href=\"http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html\"相对=nofollow> @Value 注释。有了它,你可以使用防爆pression语言,例如像这样的:

Another possibility would be the @Value annotation. With it you can use Expression Language, e.g. like this:

public DaoImpl(
    @Value("#{ systemProperties['dao.type'] }")
    final Class<?> type){
    this.type = type;
}

这篇关于更换&LT;构造带参数&GT;与Spring注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆