課程
/后端開發(fā)
/Java
/Spring入門篇
大神給個(gè)代碼唄,方便學(xué)習(xí)
2015-03-24
源自:Spring入門篇 3-1
正在回答
在com.imooc.test.base包下
lvl27
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);?}
}
舉報(bào)
為您帶來IOC和AOP的基本概念及用法,為后續(xù)高級(jí)課程學(xué)習(xí)打下基礎(chǔ)
2 回答UnitTestBase.java中isEmpty()
5 回答單元測(cè)試中的UnitTestBase.java這個(gè)類到底是用的unit3還是4,為何里面的StringUtils這個(gè)不能用
5 回答下載的代碼運(yùn)行不了是怎么回事呢?
1 回答代碼怎么寫出來的?XML文件怎么寫的?
2 回答老師的代碼 怎么運(yùn)行起來?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2015-03-27
在com.imooc.test.base包下
2017-03-20
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);
?}
}