zoukankan      html  css  js  c++  java
  • 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);
      }
    }
  • 相关阅读:
    Spring Controller参数为空串的处理方式
    netstat用法
    zookeeper的配置项
    C++ Lambda表达式用法
    java命令行运行jar里的main类
    Random的nextInt用法
    【JAVA】删除某个目录及目录下的所有子目录和文件
    Centos7设置keepAlived开机自启动
    linux设置nginx开机自启动
    window.open()方法
  • 原文地址:https://www.cnblogs.com/54hsh/p/12749848.html
Copyright © 2011-2022 走看看