使用spring JdbcDaoSupport 出现'dataSource' or 'jdbcTemplate' is required


应项目要求使用J2SE 集成 spring ,在应用JDBCTemplate进行Dao操作时,出现如下异常

Exception in thread "main" java.lang.ExceptionInInitializerError
	at hk.egame.test.Test.main(Test.java:10)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDao' defined in file [F:\workspace\pay\applicationContext-bean.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
	at hk.egame.frame.util.ContextUtil.<clinit>(ContextUtil.java:14)
	... 1 more
Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
	at org.springframework.jdbc.core.support.JdbcDaoSupport.checkDaoConfig(JdbcDaoSupport.java:109)
	at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)
	... 16 more



上代码(只贴上相关类,方便阅读)

DaoBase.java
public class DaoBase extends JdbcDaoSupport{
	
}


DaoImpl.java
public class DaoImpl extends DaoBase implements DaoInter{
	@Override
	public void delete(User t) {...}

	@Override
	public void deleteById(int id) {...}
}


****** 配置文件 ******
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
......
</bean>
 	
<bean id="daoBase" class="hk.egame.frame.base.DaoBase">
   <property name="dataSource" ref="dataSource" />
</bean>
        
<bean id="testDao" class="hk.egame.test.imp.DaoImpl">
</bean>



出错后,第一感觉是配置应该是没问题的,基类有做dataSource注入配置,查资料也未果,后无意在testDao中也注入dataSource,运行正常,经查资料, 在子类Bean中也需设置父类属性,也可以修改或者覆盖父类的属性值


修改后配置
<bean id="daoBase" class="hk.egame.frame.base.DaoBase">
   <property name="dataSource" ref="dataSource" />
</bean>
        
<bean id="testDao" class="hk.egame.test.imp.DaoImpl" parent="daoBase">
</bean>

你可能感兴趣的:(java,spring,JdbcTemplate,JdbcDaoSupport)