钻石咖啡狗
2016-07-07 10:19使用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条回答
为你推荐
- idea中创建springboot中用@Value读取.yml文件无法读取里面的数据
- spring
- maven
- intellij-idea
- ide
- 9个回答
- 制作starter时报No qualifying bean of type 'org.beetl.ext.spring.BeetlSpringViewResolver' available:
- spring
- java
- maven
- intellij-idea
- 1个回答
- Springboot启动报错 一个是找不到url 一个是找不到dao
- spring
- java
- maven
- intellij-idea
- java-ee
- 1个回答
- idea配置spring-boot,引入的test路径找不到
- spring
- maven
- intellij-idea
- 1个回答
- 大家过来看一看,瞧一瞧,有的spring报找不到xxxbean。。。纠结中!
- struts
- spring
- hibernate
- 0个回答