获取当前的spring容器ApplicationContext

来源:互联网 发布:手绘漫画的软件 编辑:程序博客网 时间:2024/04/28 15:14

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

0 0
原创粉丝点击