您的位置:首页 > 移动开发

通过ServletContextEvent加载spring的application.xml

2015-09-14 10:30 573 查看
1. 配置

  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>com.aspire.mms.sqsjb.rtgs_webapp.servlet.InitializationListener</listener-class>
</listener>

2加载

   public void contextInitialized(ServletContextEvent servletContextEvent) {

    LogAgent.sysRuntimeLogger.info("Real-Time Generate Service startup.");

   

    ApplicationContext applicationContext = (ApplicationContext) servletContextEvent.getServletContext().getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

   

    LogAgent.sysRuntimeLogger.info("Get spring application context {}", applicationContext);

   

    servletContextEvent.getServletContext().setAttribute("app", applicationContext);

    }

 public void contextInitialized(ServletContextEvent event)  

    {  

          //获取applicationContext文件所在的路径;  

          String path = event.getServletContext().getRealPath(File.separator)  

                    + event.getServletContext().getInitParameter("applicationContextConfigName");  

  

          // 把applicationContext.xml文件路径存在application作用域中;  

          event.getServletContext().setAttribute("applicationContextPath",path);      

              

          ....  

    }  

         

3 使用 

 app = (ApplicationContext) getServletConfig().getServletContext().getAttribute("app");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息