• spring单元测试容器启动类


    package com.sand.spring.util;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    import org.springframework.beans.BeansException;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.util.StringUtils;
    
    /**
     * 功能说明:spring容器启动类
     * 开发人员:@author liusha
     * 开发日期:2020/3/30 15:34
     * 功能描述:用于加载spring的配置文件
     */
    @RunWith(BlockJUnit4ClassRunner.class)
    public class SpringBootStrap {
      private String springXmlPath;
      private ClassPathXmlApplicationContext context;
    
      public SpringBootStrap() {
    
      }
    
      public SpringBootStrap(String springXmlPath) {
        this.springXmlPath = springXmlPath;
      }
    
      @Before
      public void before() {
        if (StringUtils.isEmpty(springXmlPath)) {
          springXmlPath = "classpath*:application-context.xml";
        }
        try {
          context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\s]+"));
          context.start();
          System.out.println("容器启动完成!");
        } catch (BeansException e) {
          System.out.println("容器启动失败!");
          e.printStackTrace();
        }
      }
    
      @After
      public void after() {
        System.out.println("关闭容器!");
        context.destroy();
      }
    
      protected <T extends Object> T getBean(String name) {
        return (T) context.getBean(name);
      }
    
      protected <T extends Object> T getBean(Class<?> clz) {
        return (T) context.getBean(clz);
      }
    }
  • 相关阅读:
    Swift中的可选链与内存管理(干货系列)
    Swift中的类型转换
    Swift中类与结构的初始化
    Swift3中函数的使用
    Java常用的公共方法
    Eclipse中添加文档注释快捷键
    SVN服务器的搭建(三)
    SVN服务器的搭建(二)
    SVN服务器的搭建(一)
    多线程常见的例子
  • 原文地址:https://www.cnblogs.com/54hsh/p/12749848.html
Copyright © 2020-2023  润新知