替换<构造函数-arg>带 Spring 注解构造函数-arg>
有没有办法用注释替换构造函数参数?
我有这个构造函数:
public GenericDAOImpl(Class<T> type) {
this.type = type;
}
我需要将其注入到我的外观中:
@Inject
private GenericDAO<Auto, Long> autoDao;
问题是我不知道如何在构造函数中传递参数的值。
预先感谢您
[更多信息] 我尝试解释我的问题。
<bean id="personDao" class="genericdao.impl.GenericDaoHibernateImpl">
<constructor-arg>
<value>genericdaotest.domain.Person</value>
</constructor-arg>
</bean>
我想仅使用注释来转换该代码。 有人可以解释一下怎么做吗?
there is a way to replace constructor-arg with Annotation?
I have this constructor:
public GenericDAOImpl(Class<T> type) {
this.type = type;
}
and i need to inject that in my Facade:
@Inject
private GenericDAO<Auto, Long> autoDao;
The problem is that i don't know how to pass the value of parameter in costructor.
Thank you in advance
[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>
I want convert that code using only annotation.
Someone can explain how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为仅
@Inject
没有帮助,您还必须使用@Qualifier
注释。这是 Spring 参考的相关部分:
< strong>3.9.3 使用限定符微调基于注释的自动装配
如果我理解正确的话,您将必须使用
@Qualifier
机制。如果您使用 Spring 的
@Qualifier
注释,您可能可以内联执行,如下所示:但是如果您使用 JSR-330
@Qualifier
注释,我想您必须创建自己的自定义注释,并标记为@Qualifier
。另一种可能性是
@Value
注释。有了它,您可以使用表达式语言,例如:I think
@Inject
alone won't help, you will have to use a@Qualifier
annotation also.Here's the relevant Section of the Spring Reference:
3.9.3 Fine-tuning annotation-based autowiring with qualifiers
If I understand this correctly, you will have to use the
@Qualifier
mechanism.If you use Spring's
@Qualifier
annotation, you can probably do it inline, something like this:But if you use the JSR-330
@Qualifier
annotation, I guess you will have to create your own custom annotation that is marked with@Qualifier
.Another possibility would be the
@Value
annotation. With it you can use Expression Language, e.g. like this:更新:恐怕不可能做你想做的事。您无法从注入点的参数中获取构造函数参数。
FactoryBean
将是第一个查看的地方,但它没有给出注入点元数据。 (需要注意的是:这种情况很容易被 CDI 覆盖)原始答案:(如果您在外部配置类型,这可能仍然有效)
只需在构造函数上使用
@Inject
即可。但请注意,spring 不赞成构造函数注入。考虑设置器/字段注入。然而,在您的情况下,您可能有多个
Class
类型的 bean。如果是这种情况,您可以使用@Resource(name="beanName")
。来自
javax.inject.Inject
:
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
:在构造函数中包含该类型的一个选项是:
但必须为每个 T 有特定的不同实现。
优点是您不需要将 T 类型作为参数传递。
An option to have the type in your constructor is:
but MUST have specific different implementations for each T.
Advantage is that you don't need to pass T type as a parameter.
Spring 的 Java 配置可能会有所帮助。如果您创建一个使用注释
@Configuration
和@Bean
简单定义您的 bean 的 Java 类,它可能看起来像这样:确保
DaoConfiguration
code> 类被扫描(通常通过@ComponentScan
),并且将在 Spring 上下文中为您创建一个正确的 DAO 对象。该 bean 将具有方法的名称,在本例中为personDao
,因此您可以使用名称personDao
或 按名称注入它>按类型(如果类型为GenericDAO
)。Spring's Java Configuration might be of help here. If you create a Java class that simply defines your beans using the annotations
@Configuration
and@Bean
it could look something like this:Make sure that the
DaoConfiguration
class is scanned (typically via@ComponentScan
) and a proper DAO-object will be created for you in the Spring context. The bean will have the name of the method which in this case ispersonDao
so you can inject it by name using the namepersonDao
or by type if the type isGenericDAO<Person>
.