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