问题形容

“Error creating bean with name 'userDao': Unsatisfied dependency expressed through field 'jdbcTemplate'”

形似: Error creating bean with name “xxx”: unsatisfied dependency expressed throuh field “xxxx”

⚠️ 谬误状态下的 userDaojdbcTemplate下是有红线报错的!

谬误剖析

上述表明: 没有创立 userDao的 bean,不满足 依赖字段“jbdcTemplate”

没有设置 注解扫描器,则 扫描包时,无奈注入注解到ICO容器

次要起因是 存在两个 spring配置文件,此时须要应用的 是 另一个,而另一个没有写扫描器和其余bean配置

  <!--        创立dataSource数据源-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>    <!--        创立spring对jdbc反对的工具类 jdbcTemplate-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"  />    </bean>

⚠️ UserDao是jdbcTemplate实现的,如果没有写上述代码,就没有dataSource和 jdbcTemplate,所以 userDao就没有

♂️: 这是应用spring来实现jdbc的配置,注入的形式 而不是 应用java原生的连贯jdbc

问题解决

增加相干 依赖

 <!--        创立dataSource数据源-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>    <!--        创立spring对jdbc反对的工具类 jdbcTemplate-->    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"  />    </bean>

总结就是 依赖 有无的问题


参考: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService

相干文章:

  • Error creating bean with name报错Unsatisfied dependency expressed through field
  • 解决IntelliJ IDEA Could not autowire. No beans of UserMapper type found
  • UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency