我按照老师的做法,在dao的bean中配置注入dataSource属性,结果报错:java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required,我使用的是spring-4.3.5,我猜想是jar包版本的问题

来源:4-1 转账环境的搭建

慕粉3169703

2017-02-12 14:32

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)

at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)

at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDaoImpl' defined in file [E:\workspace\Spring\target\classes\com\zxt\transaction\dao\AccountDaoImpl.class]: 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:1589)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)

at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)

at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)

at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)

... 25 more

Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

at org.springframework.jdbc.core.support.JdbcDaoSupport.checkDaoConfig(JdbcDaoSupport.java:111)

at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585)

... 40 more


写回答 关注

8回答

  • 有一个昵称
    2017-10-13 12:32:18

    或者dao层直接继承JdbcTemplate类

  • 有一个昵称
    2017-10-13 12:20:42

    * JdbcDaoSupport类中有JdbcTemplate属性 

     * 而且该类没有无参的构造方法 ,因此当创建JdbcDaoSupport对象时,只能利用有参数的构造方法

     * 所以首先应该位属性赋值 


  • 慕粉1614168191
    2017-05-04 17:40:08

    log4j.properties有什么用呢   我没有写

  • 慕粉3169703
    2017-02-12 15:02:48

    http://img.mukewang.com/58a0086600019d9f03550573.jpg   

    所有的jar包

  • 慕粉3169703
    2017-02-12 14:54:36

    我是用Maven创建的一个工程,然后在pom.xml文件中导入的jar包依赖,管理这些jar包,pom也没有出现红叉

  • 慕粉3169703
    2017-02-12 14:45:57

    增加的JdbcTemplate这个类的配置:

    <!-- 配置jdbcTemplate -->

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

      <constructor-arg ref="dataSource" />

    </bean>

  • 慕粉3169703
    2017-02-12 14:41:05

    后来我在Spring配置文件中增加了JdbcTemplate的配置,将dataSource作为属性值注入进去,然后在dao源代码中配置了:@Resource的一个方法,调用父类的setJdbcTemplate方法,最后能够成功运行,但是我还是不明白为什么直接配置dataSource属性不行呢,难道是某个jar包的版本不匹配?

  • 慕粉3169703
    2017-02-12 14:36:35

    我特意查看了源代码文件,发现JdbcDaoSupport这个类的代码基本上一致,按理说应该能注入成功,获得一个JdbcTemplate对象,但是后台报错推断出JdbcTemplate这个对象没有创建成功。

Spring事务管理

事务管理是Spring重要的知识,应用事务解决数据不一致问题

87318 学习 · 197 问题

查看课程

相似问题