使用JUnit4 测试spring时 报找不到bean的错误

使用JUnit4 测试spring时 报找不到bean的错误

在学习《spring 3.x 企业应用实战》第二节的demo时,自己用maven来构建的案例,
按照书中的讲解完成了持久层与业务层之后使用JUnit4来测试代码时发生了问题:

报错信息:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.feng.service.TestUserService': Injection of autowired dependencies failed;

项目结构:
图片说明

业务层:
图片说明
JUnit测试类:
图片说明
配置文件(applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- ①扫描类包,将标注spring注解的类自动转化Bean,同时将Bean注入 -->

<!-- ②定义一个使用DBCP实现的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
    p:url="jdbc:mysql://localhost:3306/sampledb" p:username="root"
    p:password="root" />

<!-- ③定义jdbc的模板Bean -->
<bean id="jdbcTemplate"
    class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"
 />

<!-- 配置事务管理器 -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    p:dataSource-ref="dataSource" />

<!-- 通过AOP配置提供事务增强,让service包下的Bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
    <aop:pointcut expression="execution(*com.feng.service..*(..))"
        id="serviceMethod" />
    <aop:advisor advice-ref="serviceMethod" advice-ref="serviceMethod" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

3个回答

按赞数排序
qq_19381271
qq_19381271   2016.08.08 19:48

我已经解决问题了:

maven 项目的 src/test/java中配置的测试类默认扫描src/test/resources下的配置文件;
也就是说

 @ContextConfiguration(locations={"classpath*:/config/applicationContext.xml"}) 

访问的是src/test/resources中的config/applicationContext.xml,然而我的src/test/resources中并没有applicationContext.xml文件
所以才会找不到,因而产生错误。

大概问题就是这样,可能我并没有描述清楚,如果各位有更好的解答,希望不吝赐教


你的applicationContext.xml在WEB-INF下,不是在classpath下


我已经解决问题了:

maven 项目的 src/test/java中配置的测试类默认扫描src/test/resources下的配置文件;
也就是说

 @ContextConfiguration(locations={"classpath*:/config/applicationContext.xml"}) 

访问的是src/test/resources中的config/applicationContext.xml,然而我的src/test/resources中并没有applicationContext.xml文件
所以才会找不到,因而产生错误。

大概问题就是这样,可能我并没有描述清楚,如果各位有更好的解答,希望不吝赐教


猜你喜欢

转载自blog.csdn.net/xiaobing_122613/article/details/80313338