读书人

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

发布时间: 2012-08-30 09:55:54 作者: rapoo

在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");
??
?}

读书人网 >编程

热点推荐