小小阳
2015-03-24 13:44
大神给个代码呗,方便学习
在com.imooc.test.base包下
package com.test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String stringXmlPath;
public UnitTestBase() {};
public UnitTestBase(String stringXmlPath) {
this.stringXmlPath = stringXmlPath;
};
@Before
public void before(){
if(stringXmlPath==null||stringXmlPath.equals("")){
stringXmlPath = "classPath*:spring-*.xml";
}
try{
context = new ClassPathXmlApplicationContext(stringXmlPath.split("[,\\s]+"));
context.start();
}catch(Exception e){
e.printStackTrace();
}
}
@After
public void after(){
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object>T getBean(String beanId){
return (T)context.getBean(beanId);
}
protected <T extends Object>T getBean(Class<T> clazz){
return (T)context.getBean(clazz);
}
}
Spring入门篇
268786 学习 · 963 问题
相似问题