替换<构造函数-arg>带 Spring 注解

发布于 2024-10-10 19:06:19 字数 597 浏览 9 评论 0原文

有没有办法用注释替换构造函数参数?

我有这个构造函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别在捏我脸啦 2024-10-17 19:06:19

我认为仅 @Inject 没有帮助,您还必须使用 @Qualifier 注释。

这是 Spring 参考的相关部分:
< strong>3.9.3 使用限定符微调基于注释的自动装配

如果我理解正确的话,您将必须使用 @Qualifier 机制。

如果您使用 Spring 的 @Qualifier 注释,您可能可以内联执行,如下所示:

@Repository
public class DaoImpl implements Dao{

    private final Class<?> type;

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

}

但是如果您使用 JSR-330 @Qualifier 注释,我想您必须创建自己的自定义注释,并标记为@Qualifier


另一种可能性是 @Value 注释。有了它,您可以使用表达式语言,例如:

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

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:

@Repository
public class DaoImpl implements Dao{

    private final Class<?> type;

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

}

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:

public DaoImpl(
    @Value("#{ systemProperties['dao.type'] }")
    final Class<?> type){
    this.type = type;
}
め可乐爱微笑 2024-10-17 19:06:19

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

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

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

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

来自 javax.inject.Inject

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

 @Inject ConstructorModifiersopt SimpleTypeName(FormalParameterListopt)  
   Throwsopt 构造函数主体

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
沐歌 2024-10-17 19:06:19

在构造函数中包含该类型的一个选项是:

public abstract class GenericDAO<T> {
    private Class<T> persistentClass;

    public GenericDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
            .getGenericSuperclass()).getActualTypeArguments()[0];
    }
...
}

但必须为每个 T 有特定的不同实现。

优点是您不需要将 T 类型作为参数传递。

An option to have the type in your constructor is:

public abstract class GenericDAO<T> {
    private Class<T> persistentClass;

    public GenericDAO() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
            .getGenericSuperclass()).getActualTypeArguments()[0];
    }
...
}

but MUST have specific different implementations for each T.

Advantage is that you don't need to pass T type as a parameter.

℉絮湮 2024-10-17 19:06:19

Spring 的 Java 配置可能会有所帮助。如果您创建一个使用注释 @Configuration@Bean 简单定义您的 bean 的 Java 类,它可能看起来像这样:

@Configuration
public class DaoConfiguration {
    @Bean
    public GenericDAO<Person> personDao() {
        return new GenericDaoHibernateImpl(Person.class);
    }
}

确保 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:

@Configuration
public class DaoConfiguration {
    @Bean
    public GenericDAO<Person> personDao() {
        return new GenericDaoHibernateImpl(Person.class);
    }
}

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 is personDao so you can inject it by name using the name personDao or by type if the type is GenericDAO<Person>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文