Proper way to inject parent class dependencies with Spring annotations

对着背影说爱祢 提交于 2019-12-12 11:08:45

问题


I have following code -

Dao.java

@Component
public class Dao extends NamedParameterJdbcDaoSupport {

}

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

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

The problem is that NamedParameterJdbcDaoSupport needs data source in order to work. 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? 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. I'd like to know the best that can be done using annotations only.


回答1:


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 {
}



回答2:


I've searched for something similar when using Spring's Hibernate support. 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.

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.




回答3:


This can be done transparently using xml configuration. If you want to use annotations, calling super like you are now is probably the best way to go.




回答4:


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. Since you are autowiring, you need neither. Make it protected in the parent and autowire it. Your child will have a reference to it.



来源:https://stackoverflow.com/questions/16733509/proper-way-to-inject-parent-class-dependencies-with-spring-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!