在学习《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>