junit加载配置文件出错

错误信息如下:

 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testServiceImpl': Unsatisfied dependency expressed through field 'userInfoDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userInfoDao' defined in file [E:\EclipseWorkspace\mvn-springmvc\target\classes\com\dao\user\UserInfoDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/applicationContext-dao.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/applicationContext-dao.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory

applicationContext-service.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <!-- 扫描包 controller,service-->
    <context:component-scan base-package="com.serviceimpl"></context:component-scan>

</beans>

applicationContext-dao.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
    default-lazy-init="false">

    <!-- 加载资源文件 -->
    <context:property-placeholder ignore-resource-not-found="true" location="classpath:conf/*.properties"/>

    <!-- 使用spring 加载jdbc驱动 -->
    <bean id="dataSourceTemplate"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${uname}" />
        <property name="password" value="${password}" />
    </bean>

    <!-- 数据连接池 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <property name="poolProperties">
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
                <!-- 通过引用加载数据库信息 -->
                <property name="dataSource" ref="dataSourceTemplate" />
                <property name="jmxEnabled" value="true" />
                <property name="testWhileIdle" value="true" />
                <property name="testOnBorrow" value="true" />
                <property name="testOnReturn" value="false" />
                <property name="validationInterval" value="30000" />
                <property name="validationQuery" value="SELECT 1" />
                <property name="timeBetweenEvictionRunsMillis" value="30000" />
                <property name="logAbandoned" value="false" />
                <property name="removeAbandoned" value="true" />
                <property name="removeAbandonedTimeout" value="60" />
                <!-- 初始化连接大小 -->
                <property name="initialSize" value="10"></property>
                <!-- 连接池最大数量 -->
                <property name="maxActive" value="200"></property>
                <!-- 连接池最小空闲 -->
                <property name="minIdle" value="10"></property>
                <!-- 获取连接最大等待时间 -->
                <property name="maxWait" value="60000"></property>
                <property name="jdbcInterceptors"
                    value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" />
            </bean>
        </property>
    </bean>

    <!-- MyBatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="typeAliasesPackage" value="com.model" />
        <!-- mybatis位置 -->
        <property name="configLocation" value="classpath:/mybatis/mybatis.xml" />
        <!-- 显式指定Mapper文件位置 -->
        <property name="mapperLocations" value="classpath:/mybatis/**/*Mapper.xml" />
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao" />
    </bean>

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

    <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

测试基类:

 package test.service;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试基类
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-service.xml","classpath:/spring/applicationContext-dao.xml","classpath:/spring/applicationContext-redis.xml"})
public class BaseTest {
    // "classpath:/spring/applicationContext-redis.xml"
    // extends AbstractTransactionalJUnit4SpringContextTests 需要回滚时继承
}

测试类

 package test.service.test;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.service.test.TestService;

import test.service.BaseTest;

public class TestServiceTest extends BaseTest {

    @Autowired
    private TestService testService;

    @Test
    public void test() {
        System.out.println(testService.getUserInfo().getUserName());
    }
}

说明:项目能正常启动,正常运行。但是juint测试的时候就出问题了。
到现在都没找出原因。欲哭无泪啊!求帮助

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐