首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

在JUnit中加载Spring配置文件的模式

2012-08-30 
在JUnit中加载Spring配置文件的方式@BeforeClass?public static void loadContext() {????//加载单个sprin

在JUnit中加载Spring配置文件的方式

@BeforeClass
?public static void loadContext() {
??
??//加载单个spring文件
??ApplicationContext context00 = new ClassPathXmlApplicationContext("spring-global-db.xml");
??
??//加载类路径下的配置文件ClassPathXmlApplicationContex
??ApplicationContext?context0 = new ClassPathXmlApplicationContext(
????new String[]{"spring-global-db.xml",
????????"spring-dao.xml",
????????"spring-service.xml"});

??//加载文件系统下的路径中的配置文件
??String basePath = System.getProperty("user.dir");
??String db = basePath+"/WebRoot/WEB-INF/spring/spring-global-db.xml";
??String dao = basePath+"\\WebRoot\\WEB-INF\\spring\\spring-dao.xml";
??String service = basePath+"\\WebRoot\\WEB-INF\\spring\\spring-service.xml";
??ApplicationContext context1 =
???new FileSystemXmlApplicationContext(new String[]{db, dao, service});
??
??//用WEB应用的路径加载spring配置文件
??XmlWebApplicationContext?context2 = new XmlWebApplicationContext();
??context2.setConfigLocations(new String[]{
???"/WEB-INF/spring/spring-global-db.xml",
???"/WEB-INF/spring/spring-dao.xml",
???"/WEB-INF/spring/spring-service.xml"
??});
??//需要servletContext变量,一般由request.getSession().getSersvletContext()获取
??context2.setServletContext(request.getSession().getServletContext());
??context2.refresh();
??
??manageEmpServiceImpl?= (ManageEmpServiceImpl) context1.getBean("manageEmpServiceImpl");
??
?}

热点排行