方法一:(获取当前的spring容器,任何java类中适用)
ServletContext application = ServletActionContext.getServletContext();
ApplicationContext act = ContextLoader.getCurrentWebApplicationContext();UserService userService = (UserService) act.getBean("userService");
方法二:(重新加载spring容器)
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
方法三:(通过request或session加载spring容器)
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());