SSH中获取ApplicationContext 对象

如果是单独测试则根据以下获取ApplicationContext对象

ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml","spring-hibernate.xml")

//获取某个service接口

UserServiceI userServiceI = (UserServiceI)ac.getBean("userService");

使用上面方法会重新加载,不建议在Web项目中使用   使用下面方法

ServletContext sc = request.getSession().getServletContext();
 ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc); 
 AlipayServiceI alipayServiceI = (AlipayServiceI)ac.getBean("alipayService");

 

posted @ 2014-10-30 11:11  吉利来  阅读(392)  评论(0编辑  收藏  举报