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);
      }
    }
  • 相关阅读:
    数据库-数据约束
    数据库-表2
    数据库-表
    MySQL入门
    记一次stm8l程序跑飞
    nRF24L01P的ShockBurst与Enhance ShockBurst
    电路板工艺中的NPTH和PTH
    nRF24L01P数据传输速率
    STM32F030-UART1_DMA使用提示
    Altium Designer 复制报错-奇怪的问题解决办法
  • 原文地址:https://www.cnblogs.com/54hsh/p/12749848.html
Copyright © 2011-2022 走看看