org.springframework.web.struts
public class: ContextLoaderPlugIn [javadoc |
source]
java.lang.Object
org.springframework.web.struts.ContextLoaderPlugIn
All Implemented Interfaces:
org.apache.struts.action.PlugIn
Struts 1.1+ PlugIn that loads a Spring application context for the Struts
ActionServlet. This context will automatically refer to the root
WebApplicationContext (loaded by ContextLoaderListener/Servlet) as parent.
The default namespace of the WebApplicationContext is the name of the
Struts ActionServlet, suffixed with "-servlet" (e.g. "action-servlet").
The default location of the XmlWebApplicationContext configuration file
is therefore "/WEB-INF/action-servlet.xml".
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
The location of the context configuration files can be customized
through the "contextConfigLocation" setting, analogous to the root
WebApplicationContext and FrameworkServlet contexts.
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml /WEB-INF/myContext.xml"/>
</plug-in>
Beans defined in the ContextLoaderPlugIn context can be accessed
from conventional Struts Actions, via fetching the WebApplicationContext
reference from the ServletContext. ActionSupport and DispatchActionSupport
are pre-built convenience classes that provide easy access to the context.
It is normally preferable to access Spring's root WebApplicationContext
in such scenarios, though: A shared middle tier should be defined there
rather than in a ContextLoaderPlugin context, for access by any web component.
ActionSupport and DispatchActionSupport autodetect the root context too.
A special usage of this PlugIn is to define Struts Actions themselves
as beans, typically wiring them with middle tier components defined in the
root context. Such Actions will then be delegated to by proxy definitions
in the Struts configuration, using the DelegatingActionProxy class or
the DelegatingRequestProcessor.
Note that you can use a single ContextLoaderPlugIn for all Struts modules.
That context can in turn be loaded from multiple XML files, for example split
according to Struts modules. Alternatively, define one ContextLoaderPlugIn per
Struts module, specifying appropriate "contextConfigLocation" parameters.
Note: The idea of delegating to Spring-managed Struts Actions originated in
Don Brown's Spring Struts Plugin.
ContextLoaderPlugIn and DelegatingActionProxy constitute a clean-room
implementation of the same idea, essentially superseding the original plugin.
Many thanks to Don Brown and Matt Raible for the original work and for the
agreement to reimplement the idea in Spring proper!
| Field Summary |
|---|
| public static final String | DEFAULT_NAMESPACE_SUFFIX | Suffix for WebApplicationContext namespaces. If a Struts ActionServlet is
given the name "action" in a context, the namespace used by this PlugIn will
resolve to "action-servlet". |
| public static final Class | DEFAULT_CONTEXT_CLASS | Default context class for ContextLoaderPlugIn. |
| public static final String | SERVLET_CONTEXT_PREFIX | Prefix for the ServletContext attribute for the WebApplicationContext.
The completion is the Struts module name. |
| protected final Log | logger | |
| Method from org.springframework.web.struts.ContextLoaderPlugIn Summary: |
|---|
|
createWebApplicationContext, destroy, getActionServlet, getContextClass, getContextConfigLocation, getModuleConfig, getModulePrefix, getNamespace, getServletContext, getServletContextAttributeName, getServletName, getWebApplicationContext, init, initWebApplicationContext, onInit, setContextClass, setContextClassName, setContextConfigLocation, setNamespace |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.springframework.web.struts.ContextLoaderPlugIn Detail: |
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() +
"', module '" + getModulePrefix() + "' will try to create custom WebApplicationContext " +
"context of class '" + getContextClass().getName() + "', using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
throw new ApplicationContextException(
"Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() +
"', module '" + getModulePrefix() + "': custom WebApplicationContext class [" +
getContextClass().getName() + "] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
wac.setParent(parent);
wac.setServletContext(getServletContext());
wac.setNamespace(getNamespace());
if (getContextConfigLocation() != null) {
wac.setConfigLocations(
StringUtils.tokenizeToStringArray(
getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
wac.addBeanFactoryPostProcessor(
new BeanFactoryPostProcessor() {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet()));
beanFactory.ignoreDependencyType(ActionServlet.class);
}
}
);
wac.refresh();
return wac;
}
Instantiate the WebApplicationContext for the ActionServlet, either a default
XmlWebApplicationContext or a custom context class if set.
This implementation expects custom contexts to implement ConfigurableWebApplicationContext.
Can be overridden in subclasses. |
public void destroy() {
getServletContext().log("Closing WebApplicationContext of Struts ActionServlet '" +
getServletName() + "', module '" + getModulePrefix() + "'");
if (getWebApplicationContext() instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) getWebApplicationContext()).close();
}
}
Close the WebApplicationContext of the ActionServlet. |
public final ActionServlet getActionServlet() {
return actionServlet;
}
Return the Struts ActionServlet that this PlugIn is associated with. |
public Class getContextClass() {
return this.contextClass;
}
Return the custom context class. |
public String getContextConfigLocation() {
return this.contextConfigLocation;
}
Return the explicit context config location, if any. |
public final ModuleConfig getModuleConfig() {
return this.moduleConfig;
}
Return the Struts ModuleConfig that this PlugIn is associated with. |
public final String getModulePrefix() {
return this.moduleConfig.getPrefix();
}
Return the prefix of the ModuleConfig that this PlugIn is associated with. |
public String getNamespace() {
if (this.namespace != null) {
return this.namespace;
}
if (this.actionServlet != null) {
return this.actionServlet.getServletName() + DEFAULT_NAMESPACE_SUFFIX;
}
return null;
}
Return the namespace for the ActionServlet, falling back to default scheme if
no custom namespace was set: e.g. "test-servlet" for a servlet named "test". |
public final ServletContext getServletContext() {
return this.actionServlet.getServletContext();
}
Return the ServletContext that this PlugIn is associated with. |
public String getServletContextAttributeName() {
return SERVLET_CONTEXT_PREFIX + getModulePrefix();
}
|
public final String getServletName() {
return this.actionServlet.getServletName();
}
Return the name of the ActionServlet that this PlugIn is associated with. |
public final WebApplicationContext getWebApplicationContext() {
return webApplicationContext;
}
Return this PlugIn's WebApplicationContext. |
public final void init(ActionServlet actionServlet,
ModuleConfig moduleConfig) throws ServletException {
long startTime = System.currentTimeMillis();
if (logger.isInfoEnabled()) {
logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() +
", module '" + moduleConfig.getPrefix() + "': initialization started");
}
this.actionServlet = actionServlet;
this.moduleConfig = moduleConfig;
try {
this.webApplicationContext = initWebApplicationContext();
onInit();
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
throw ex;
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() +
"', module '" + moduleConfig.getPrefix() + "': initialization completed in " + elapsedTime + " ms");
}
}
Create the ActionServlet's WebApplicationContext. |
protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException {
getServletContext().log("Initializing WebApplicationContext for Struts ActionServlet '" +
getServletName() + "', module '" + getModulePrefix() + "'");
WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = createWebApplicationContext(parent);
if (logger.isInfoEnabled()) {
logger.info("Using context class '" + wac.getClass().getName() + "' for servlet '" + getServletName() + "'");
}
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
if (logger.isDebugEnabled()) {
logger.debug("Published WebApplicationContext of Struts ActionServlet '" + getServletName() +
"', module '" + getModulePrefix() + "' as ServletContext attribute with name [" + attrName + "]");
}
return wac;
}
Initialize and publish the WebApplicationContext for the ActionServlet.
Delegates to #createWebApplicationContext for actual creation.
Can be overridden in subclasses. Call getActionServlet()
and/or getModuleConfig() to access the Struts configuration
that this PlugIn is associated with. |
protected void onInit() throws ServletException {
}
Callback for custom initialization after the context has been set up. |
public void setContextClass(Class contextClass) {
this.contextClass = contextClass;
}
Set a custom context class. This class must be of type WebApplicationContext,
when using the default ContextLoaderPlugIn implementation, the context class
must also implement ConfigurableWebApplicationContext. |
public void setContextClassName(String contextClassName) throws ClassNotFoundException {
this.contextClass = ClassUtils.forName(contextClassName);
}
Set a custom context class by name. This class must be of type WebApplicationContext,
when using the default ContextLoaderPlugIn implementation, the context class
must also implement ConfigurableWebApplicationContext. |
public void setContextConfigLocation(String contextConfigLocation) {
this.contextConfigLocation = contextConfigLocation;
}
Set the context config location explicitly, instead of relying on the default
location built from the namespace. This location string can consist of
multiple locations separated by any number of commas and spaces. |
public void setNamespace(String namespace) {
this.namespace = namespace;
}
Set a custom namespace for the ActionServlet,
to be used for building a default context config location. |