方法一:(获取当前的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());

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐