简体   繁体   English

使用Spring注释注入父类依赖项的正确方法

[英]Proper way to inject parent class dependencies with Spring annotations

I have following code - 我有以下代码 -

Dao.java Dao.java

@Component
public class Dao extends NamedParameterJdbcDaoSupport {

}

dbContext.xml dbContext.xml

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.jdbc.url}" />
            <property name="username" value="${db.user}" />
            <property name="password" value="${db.password}" />
        </bean>

applicationContext.xml applicationContext.xml中

<context:component-scan base-package="com.kshitiz" />

The problem is that NamedParameterJdbcDaoSupport needs data source in order to work. 问题是NamedParameterJdbcDaoSupport需要数据源才能工作。 Since this is a property of the super class and not my own class the only way I could think of to make it work is - 既然这是超类的属性而不是我自己的类,我能想到的唯一方法就是 -

@Component
public class Dao extends NamedParameterJdbcDaoSupport {
    @Autowired
    public void setDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
}

This is pretty ugly. 这非常难看。 Can I specify that I want to autowire all the properties of my bean? 我可以指定我想要自动装配我的bean的所有属性吗? Something like - 就像是 -

@Component(default-autowire="byType")
public class Dao extends NamedParameterJdbcDaoSupport {

}

Is this possible in Spring? 这在春天有可能吗? Alternatively what is the most elegant way to inject super class dependencies? 或者,注入超类依赖项的最优雅方法是什么?

Edit: I already know this can be done using XML which I am presently using. 编辑:我已经知道这可以使用我目前正在使用的XML来完成。 I'd like to know the best that can be done using annotations only. 我想知道仅使用注释可以做到的最好。

Not necessarily the answer you were looking for, but I would do this with an intermediary super class. 不一定是你要找的答案,但我会用中级超级班做这个。

public abstract class AbstractDao extends NamedParameterJdbcDaoSupport {
    @Autowired
    public void setDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
}

@Component
public class Dao extends AbstractDao {
}

I've searched for something similar when using Spring's Hibernate support. 在使用Spring的Hibernate支持时,我搜索了类似的东西。 There's no way of adding (or changing) the wiring in a superclass without subclassing and overriding the required method. 没有子类化并覆盖所需方法,就无法在超类中添加(或更改)布线。 Or the declarative approach of subclassing and providing a ref value for the desired properties via XML. 或者通过XML子类化并为所需属性提供ref值的声明性方法。

Anything less "ugly" would probably be less transparent. 任何不那么“丑陋”的东西都可能不太透明。 So Zutty's proposed solution fits best here as it eliminates the need of overriding in each Dao implementation. 所以Zutty提出的解决方案最适合这里,因为它消除了在每个Dao实现中覆盖的需要。

This can be done transparently using xml configuration. 这可以使用xml配置透明地完成。 If you want to use annotations, calling super like you are now is probably the best way to go. 如果你想使用注释,像现在这样调用super可能是最好的方法。

If it is required for your class to work (and it probably is in a DAO), it should be a constructor argument not a property. 如果您的类需要工作(并且它可能在DAO中),那么它应该是构造函数参数而不是属性。 Since you are autowiring, you need neither. 既然你是自动装配,你也不需要。 Make it protected in the parent and autowire it. 使其在父级中受到保护并自动装配它。 Your child will have a reference to it. 你的孩子会参考它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM