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");

原文地址:https://www.cnblogs.com/zhuangjixiang/p/4062117.html