Reputation: 51
I'm trying to Inject(Autowire) JDBCTemplate into my Dao Class which is an "Abstract class", This is not working and as spring is giving null bean for JDBCTemplate.
public abstract class SSODaoImpl extends NamedParameterJdbcDaoSupport implements SSODao{
public SSODaoImpl(){
}
@Autowired //giving null jdbcTemplate
public SSODaoImpl(JdbcTemplate jdbcTemplate){
super.setJdbcTemplate(jdbcTemplate);
}
}
SSODaoImpl is extended my many other DAOs' like the one below
@Repository("askBenefitsDAO")
public class AskBenefitsSSODaoImpl extends SSODaoImpl{
}
I have created the bean in a file JDBCContext.xml and referenced it in web.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jndi/hpdb_hrdb"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
Web.xml
<context-param>
<param-name> /WEB-INF/spring/JDBCTemplate/JDBCContext.xml</param-value>
</context-param>
Error message from spring while starting the application
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'askBenefitsDAO' defined in file [AskBenefitsSSODaoImpl.class]:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
The above setup is working for an "Non Abstract class" set up. Please help me with this and let me know what I'm doing wrong
Upvotes: 1
Views: 1246
Reputation: 3709
The reason is because the spring is not directly invoking the constructor of your SSODaoImpl class, instead the call of its constructor is happening when Spring is instantiating the AskBenefitsSSODaoImpl class and hence Spring is unable to bind the jdbcTemplate to your SSODaoImpl class.
You can achieve this my modifying your code to as below:
@Repository("askBenefitsDAO")
public class AskBenefitsSSODaoImpl extends SSODaoImpl{
@Autowired
public AskBenefitsSSODaoImpl(JdbcTemplate jdbcTemplate){
super(jdbcTemplate);
}
}
public abstract class SSODaoImpl extends NamedParameterJdbcDaoSupport implements SSODao{
public SSODaoImpl(){
}
public SSODaoImpl(JdbcTemplate jdbcTemplate){
super.setJdbcTemplate(jdbcTemplate);
}
}
Upvotes: 2