PDA

View Full Version : FrameworkServlet.initWebApplicationContext() BUG?


Athrawn17
Oct 7th, 2005, 06:39 PM
In the FrameworkServlet.initWebApplicationContext() method the context is published as a servlet context attribute. Below is the relevant code.


protected WebApplicationContext initWebApplicationContext(){
...
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
...
}
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getServletName();
}

public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT.";


This is fine, except that when I use the static method


WebApplicationContextUtils.getWebApplicationContex t(myServletContext);


to get the context this is the code that gets executed:


public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APP LICATION_CONTEXT_ATTRIBUTE);
...
}

String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class + ".ROOT";


Obviously there is a disconnect here. The FrameworkServlet should use the same parameter when setting the servlet context attribute as the Util class is using to obtain it.

Is this a bug or is there a reason that the Framework Servlet is doing this? Note that the ContextLoader class also uses the ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE when publishing the context.

davidhedley
Oct 13th, 2005, 10:33 AM
This isn't a bug. WebApplicationContextUtils.getWebApplicationContex t returns the root application context. The servlet application context created by FrameworkServlet is a child of this context. The root application context is created by ContextLoaderListener or null if not present.