通过 ServletContext 获取 WebApplicationContext

来源:互联网 发布:天猫淘宝买手机靠谱吗 编辑:程序博客网 时间:2024/05/18 13:27

在视图或者 struts 中通过 ServletContext 获取 WebApplicationContext,三个方法如下,英文很简单我就不用再翻译了:

  • getRequiredWebApplicationContext

    public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)                                                              throws IllegalStateException
    Find the root WebApplicationContext for this web application, which is typically loaded via ContextLoaderListener.

    Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.

    Parameters:
    sc - ServletContext to find the web application context for
    Returns:
    the root WebApplicationContext for this web app
    Throws:
    IllegalStateException - if the root WebApplicationContext could not be found
    See Also:
    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  • getWebApplicationContext

    public static WebApplicationContext getWebApplicationContext(ServletContext sc)
    Find the root WebApplicationContext for this web application, which is typically loaded via ContextLoaderListener.

    Will rethrow an exception that happened on root context startup, to differentiate between a failed context startup and no context at all.

    Parameters:
    sc - ServletContext to find the web application context for
    Returns:
    the root WebApplicationContext for this web app, or null if none
    See Also:
    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  • getWebApplicationContext

    public static WebApplicationContext getWebApplicationContext(ServletContext sc,                                             String attrName)
    Find a custom WebApplicationContext for this web application.
    Parameters:
    sc - ServletContext to find the web application context for
    attrName - the name of the ServletContext attribute to look for
    Returns:
    the desired WebApplicationContext for this web app, or null if none

0 0
原创粉丝点击