SSM框架Maven項目使用Test註解事務層空指針異常

問題描述:在使用@Test註解後,運行註解方法,事務層爆出空指針異常。

問題處理:

首先導入以下依賴:


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>

使用test註解的控制器頂部添加註解:

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//此包需要手動導入,eclipse不支持自動導入

@RunWith(SpringJUnit4ClassRunner.class)//註解1
@ContextConfiguration({"classpath:spring.xml"})//註解2
public class TestController {
    
    @Autowired
    private AC_EntityService aC_EntityService;
    
    @Test
    public void SelectValue() {
        DatabaseEntity queryEntity = aC_EntityService.QueryEntity(1);
        System.out.println("查詢出的信息爲:"+queryEntity);
    }

……

}

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

    <!--加載jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!--JNDI獲取數據源(dbcp連接處) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" scope="singleton">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${uname}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxIdle" value="${maxIdle}" />
        <property name="minIdle" value="${minIdle}" />
        <property name="maxWait" value="${maxWait}" />
        <!--當前空閒連接數< 2 && (當前活動數>最大活動數-3) -->
        <property name="removeAbandoned" value="${removeAbandoned}" />
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
        <!--sql心跳 :保證連接池中連接是真實有效的連接 -->
        <!--開啓Evict的定時校驗,循環校驗 -->
        <property name="testWhileIdle" value="true" />
        <!-- 定義Evict的時間間隔,單位:毫秒,大於0纔會開啓evict -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 在進行borrowObject處理時,會對拿到的連接進行校驗-false不校驗 -->
        <property name="testOnBorrow" value="false" />
        <!-- 在進行returnObject處理時,會對返回的連接進行校驗-false不校驗 -->
        <property name="testOnReturn" value="false" />
        <!-- 校驗使用的sql語句,validationQuery,複雜的校驗sql會影響性能 -->
        <property name="validationQuery" value="select 1" />
        <!-- 配置每次校驗連接的數量,一般等於maxActive -->
        <property name="numTestsPerEvictionRun" value="${maxActive}" />
    </bean>
    <!--注入SqlSessionFactory對象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:configLocation="classpath:mybatis.xml" p:dataSource-ref="dataSource" />

    <!--注入映射器 批量生成映射器的實例 basePackage:指定了批量產生映射器實例實現類 StuDaoImpl -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.ssm.dao" />

    <!--注入事務管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource" />
        
    <!--配置基於註解的事務支持 -->
    <!-- <tx:annotation-driven transaction-manager="txManager"/> -->
    <!--[AOP事務支持] -->
    <!-- start -->
    <aop:aspectj-autoproxy />
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.ssm.service.*.*(..))"
            id="transService" />
        <aop:advisor pointcut-ref="transService" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="zk*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
    <!-- AOP 事務處理 結束 -->
    <!--配置基於註解掃描的Bean 將pojo實例化到Spring容器中 -->
    <context:component-scan base-package="com.ssm" />
</beans>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章