Spring整合junit出现的版本异常

Spring整合junit出现的console和junit界面无提示的异常

/**
 * @author:xhz
 * @time: 2018年8月15日
 */
package com.xhz.springAOP;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author xhz
 * @scription:增强UserServiceImpl下的方法,即添加事务
 */
public class MyAdvice {
     //前置通知
    public void before(){
        System.out.println("前置通知!");
    }

    //后置通知(注意:有异常时不执行)
    public void afterRurning(){
        System.out.println("后置通知(没异常)!");
    }

    //环绕通知(注意:必须手动调用)
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("环绕通知之前执行!");
        Object object=pjp.proceed();
        System.out.println("环绕通知之后执行!");
        return object;
    }
    //异常拦截通知
    public void afterException(){
        System.out.println("啊啊啊啊!有异常啊!");
    }
    //后置通知(注意:一定会执行的方法)
    public void after(){
        System.out.println("后置通知!");
    }
}
/**
 * @author:xhz
 * @time: 2018年8月15日
 */
package com.xhz.springAOP;

/**
 * @author xhz
 * @scription:实现用户的增删改查
 */
public interface UserService {
     public void addUser();//增
     public void delUser();//删
     public void updUser();//改
     public void queUser();//查
}
/**
 * @author:xhz
 * @time: 2018年8月15日
 */
package com.xhz.springAOP;

/**
 * @author xhz
 * @scription:实现UserService接口
 */
public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("增加用户!");

    }

    @Override
    public void delUser() {
        System.out.println("增加用户!");

    }

    @Override
    public void updUser() {
        System.out.println("增加用户!");

    }

    @Override
    public void queUser() {
        System.out.println("增加用户!");

    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://www.springframework.org/schema/beans" 
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans\spring-beans.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 ">
        <!-- 1:配置目标对象 -->                  
       <bean name="u1" class="com.xhz.springAOP.UserServiceImpl"></bean>
       <!-- 2:配置通知类 -->                    
       <bean name="myAdvice" class="com.xhz.springAOP.MyAdvice"></bean> 
       <!-- 3:配置切面类将通知织入目标对象 -->
       <aop:config>
          <!-- 配置切入点 -->
          <aop:pointcut expression="execution(* com.xhz.springAOP.*ServiceImpl.*())" id="pc"/>
          <!-- 织入通知对象方法 -->
          <aop:aspect ref="myAdvice">
              <!-- 前置通知 -->
              <aop:before method="before" pointcut-ref="pc"/>
              <!-- 后置通知 -->
              <aop:after-returning method="afterRurning" pointcut-ref="pc"/>
              <!-- 环绕通知 -->
              <aop:around method="around" pointcut-ref="pc"/>
              <!-- 异常拦截通知 -->
              <aop:after-throwing method="afterException" pointcut-ref="pc"/>
              <!-- 后置通知 -->
              <aop:after method="after" pointcut-ref="pc"/>
          </aop:aspect>
       </aop:config>      

</beans>
package com.xhz.springAOP;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
 * 测试类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/xhz/springAOP/applicationContext.xml")
public class SpringAOPDemo {
    @Resource(name="u1")
    private UserService us;

    @Test
    public void test(){
        /*@SuppressWarnings("resource")
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us=(UserService)ac.getBean("u1");*/
        us.addUser();
    }

}
   ![导入的包目录](https://img-blog.csdn.net/20180816131941154?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
   ![junit界面](https://img-blog.csdn.net/20180816132101676?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
   ![console界面](https://img-blog.csdn.net/20180816132134758?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
   经过一番摸索:我将导入的spring-test-5.0.1换成spring-test-4.2.4程序运行就正常了:
   ![这里写图片描述](https://img-blog.csdn.net/20180816132734352?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
   总结:spring的spring—test的版本应该和junit的版本保持在同一个水平。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值