Spring test bean注入问题
老师好,我在做spring jdbc作业时遇到错误无法排除,想请问该从何下手
目前编译的部份都没出错,然后我是用注解方式定义bean,不知道这是不是出错的原因
(为简化代码,已先排除import与getter/setter)
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/jdbc_imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <context:component-scan base-package="com.imooc"/> </beans>
HotelServiceTest类 (test.java中的测试类)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class HotelServiceTest extends TestCase {
@Resource
private HotelDao hotelDao;
@Test
public void testFindById(){
Hotel hotel = hotelDao.findById(10001);
System.out.println(hotel);
}
}Hotel类
public class Hotel {
private Integer orderNo;
private String city;
private Float price;
private String hotelname;
private String arrivedate;
private String leavedate;HotelDao
@Repository
public class HotelDao {
private JdbcTemplate jdbcTemplate;
@Resource
private Hotel hotel;
public Hotel findById(Integer oid){
String sql = "SELECT * FROM hotel WHERE orderNo = ?";
Hotel hotel = jdbcTemplate.queryForObject(sql, new Object[]{oid}, new BeanPropertyRowMapper<Hotel>(Hotel.class));
return hotel;
}HotelService类
@Service
public class HotelService {
@Resource
private HotelDao hotelDao;
public Hotel findById(Integer oid){
Hotel hotel = hotelDao.findById(10001);
System.out.println(hotel);
return hotel;
}
}出现错误
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.imooc.jdbc.dao.HotelDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.imooc.jdbc.service.HotelServiceTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.imooc.jdbc.dao.HotelDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
正在回答
同学你好,由于同学提供的代码不完整,老师无法进行测试。
老师可以给同学一些思路,同学可以自主尝试解决一下,如果依然无法解决,建议同学提供完整的代码(包含包结构等的完整代码)。
1.从报错信息看,是创建HotelDao对象时,对象创建失败导致的。同学可以直接在测试类中,使用Spring工厂直接获取该对象尝试一下。这样可以检查对象是否可以正常创建。
2.如果对象可以正常创建,那么可以配合注入的注解,检查对象是否是注入失败的。
3.如果确实是创建失败,可以先把JDBC部分代码注释掉,仅测试对象的创建。
祝学习愉快~
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星