钻石咖啡狗 2016-07-07 10:19 采纳率: 0%
浏览 5321

使用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条回答 默认 最新

  • 钻石咖啡狗 2016-08-08 02:42
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮