问题描述:在使用@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>