问答详情
源自:3-1 Spring Bean装配之Bean的配置项及作用域

class UnitTestBase 完整代码

class UnitTestBase 这个类就不能说一下吗? 完整的代码在哪里

提问者:慕码人7551565 2020-11-27 16:51

个回答

  • 慕侠8185743
    2020-11-29 14:56:09

    package com.imooc.test.base;


    import org.junit.After;

    import org.junit.Before;

    import org.springframework.context.support.ClassPathXmlApplicationContext;


    public class UnitTestBase {

    String springXmlPath;

    ClassPathXmlApplicationContext context;

    public UnitTestBase() {}

    public UnitTestBase(String springXmlPath){

    this.springXmlPath = springXmlPath;

    }

    @Before

    public void before(){

    if("" == springXmlPath){

    springXmlPath = "classpath*:spring-*.xml";

    }

    try{

    context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));

    context.start();

    }catch(Exception e){

    e.printStackTrace();

    }

    }

    @After

    public void  after(){

    context.destroy();

    }

    public <T extends Object > T getBean(String beanId){

    try{

    return (T) context.getBean(beanId);

    }catch(Exception e){

    e.printStackTrace();

    return null;

    }

    }


    }