通过ServletContextEvent加载spring的application.xml

来源:互联网 发布:java获取cookie的方法 编辑:程序博客网 时间:2024/05/29 04:56

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

    }


  1.  public void contextInitialized(ServletContextEvent event)  
  2.     {  
  3.           //获取applicationContext文件所在的路径;  
  4.           String path = event.getServletContext().getRealPath(File.separator)  
  5.                     + event.getServletContext().getInitParameter("applicationContextConfigName");  
  6.   
  7.           // 把applicationContext.xml文件路径存在application作用域中;  
  8.           event.getServletContext().setAttribute("applicationContextPath",path);      
  9.               
  10.           ....  
  11.     }  
  12.          
3 使用 

 app = (ApplicationContext) getServletConfig().getServletContext().getAttribute("app");


0 0
原创粉丝点击