spring注入bean500异常org.springframework.beans.factory.UnsatisfiedDependencyException:No qualifying bean

tech2022-08-28  110

首先看看异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.chiguozi.dao.Userdao’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这段话的意思是没有找到合适的bean,那么解决要怎么解决这个问题呢 1.由于是ssm项目,这个bean的配置文件有没有被加载呢? 在web.xml中需要配置

<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext.xml </param-value> </context-param> 以及 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 作为服务器启动要加载的文件

假如没有这两段东西的话,那么spring配置文件就自然不会加载,mybatis中的配置也肯定不会加载,最终就会呈现上面的错误了,解决上面错误的方法,就是加上这两段东西。

2.spring配置文件中,关于mybatis配置是否有问题,mapper是否被扫描到。 我的配置

<!--spring整合Mybatis--> <!--配置连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssmdemo"/> <property name="user" value="我是账号名"/> <property name="password" value="我是密码"/> </bean> <!--配置SqlSessionFactory工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置AccountDao接口所在地,扫描包--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.chiguozi.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>

3.检查mapper是否有加注解。楼主使用的是纯注解的

package com.chiguozi.dao; import com.chiguozi.domain.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository//这个一定不能少 public interface Userdao { @Select("select * from user") public List<User> findAll(); @Insert("insert into user(username,userpassword) values(#{username},#{userpassword})") public void saveAccount(User user); }

4.@Autowired自动注入的地方名字是否写对

package com.chiguozi.service.Impl; import com.chiguozi.domain.User; import com.chiguozi.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.chiguozi.dao.Userdao; import java.util.List; @Service("userService") public class UserServiceImpl implements UserService { @Autowired private Userdao userdao; @Override public List<User> findAll() { System.out.println("查询所有"); return userdao.findAll(); } }
最新回复(0)