org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fkhd.whiteshirt.daos.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
spring-mybatis:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="localdataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fkhd.whiteshirt.daos" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
serviceImpl:
@Autowired
private UserMapper userMapper;
修改serviceImpl爲:
@Autowired(required=false)
private UserMapper userMapper;
不報錯了,但userMapper爲null,也不注入了,這樣解決雖然不報錯了,但項目無法繼續進行,所以另尋方法。
各種解決辦法都試了後,沒解決,所以就自己在重新看看自己的項目。
我的錯誤原因在:web.xml啓動是沒加載soring-mybatis.xml,加上以後問題解決。
我的這個原因在於web.xml沒有加載,僅是問題一種。可以看看你的是否加載,如果加載了就去看看其他原因吧!
可以運行的配置:
spring-mybatis(未修改):
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="localdataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fkhd.whiteshirt.daos" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
serviceImpl(未修改):
@Autowired
private UserMapper userMapper;