Spring has three starting modes, using Contextloaderservlet,contextloaderlistener and Contextloaderplugin.
Look at the source code of Contextloaderlistener, this is a servletcontextlistener
/**
* Initialize the root Web application context.
*/
public void contextinitialized (Servletcontextevent event) {
This.contextloader = Createcontextloader ();
This.contextLoader.initWebApplicationContext (Event.getservletcontext ());
}
/**
* Create the Contextloader to use. Can is overridden in subclasses.
* @return The new Contextloader
*/
Protected Contextloader Createcontextloader () {
return new Contextloader ();
}
Source Code of Contextloader
Public Webapplicationcontext Initwebapplicationcontext (ServletContext servletcontext)
Throws Beansexception {
Long startTime = System.currenttimemillis ();
if (logger.isinfoenabled ()) {
Logger.info ("Root webapplicationcontext:initialization started");
}
ServletContext.log ("Loading Spring root Webapplicationcontext");
try {
Determine parent for root Web application context, if any.
ApplicationContext parent = Loadparentcontext (ServletContext);
Webapplicationcontext WAC = Createwebapplicationcontext (ServletContext, parent);
Servletcontext.setattribute (
Webapplicationcontext.root_web_application_context_attribute, WAC);
if (logger.isinfoenabled ()) {
Logger.info ("Using context class [" + Wac.getclass (). GetName () +
"] for root webapplicationcontext");
}
if (logger.isdebugenabled ()) {
Logger.debug ("Published root webapplicationcontext [" + WAC +
"] as ServletContext attribute with name [" +
Webapplicationcontext.root_web_application_context_attribute + "]");
}
if (logger.isinfoenabled ()) {
Long elapsedtime = System.currenttimemillis ()-startTime;
Logger.info ("Root Webapplicationcontext:initialization completed in" + ElapsedTime + "MS");
}
return WAC;
}
catch (RuntimeException ex) {
Logger.error ("Context initialization Failed", ex);
Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, ex);
Throw ex;
}
catch (Error err) {
Logger.error ("Context initialization Failed", err);
Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, err);
throw err;
}
}
Pay attention to Webapplicationcontext.root_web_application_context_attribute, this place webapplicationcontext, To be removed from ServletContext when used
You can use Webapplicationcontextutils to get Webapplicationcontext
public static Webapplicationcontext Getwebapplicationcontext (ServletContext SC) {
Object attr = Sc.getattribute (Webapplicationcontext.root_web_application_context_attribute);
if (attr = = null) {
return null;
}
if (attr instanceof runtimeexception) {
Throw (runtimeexception) attr;
}
if (attr instanceof Error) {
Throw (Error) attr;
}
if (! ( attr instanceof Webapplicationcontext)) {
throw new IllegalStateException ("Root context attribute is not of type Webapplicationcontext:" + attr);
}
Return (Webapplicationcontext) attr;
}
The key question is how struts launches the spring, Contextloaderplugin source
Publish the context as a servlet context attribute.
String attrname = Getservletcontextattributename ();
Getservletcontext (). SetAttribute (Attrname, WAC);
Public String Getservletcontextattributename () {
return Servlet_context_prefix + getmoduleprefix ();
}
Different loaded key is different, the reason is webapplicationcontext put in the problem, but spring call will be based on the name defined in the Webapplicationcontext, the problem is found here
Configuring in Struts-config.xml
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >
<set-property property= "contextconfiglocation" value= "/web-inf/applicationcontext.xml"/>
</plug-in>
<controller>
<set-property property= "Processorclass" value= "Org.springframework.web.struts.DelegatingRequestProcessor"/ >
</controller>
The principle is this, struts can have only one actionservlet instance, but for different sub-applications can have their own requestprocessor instance each requestprocessor instance corresponding to different struts configuration files respectively.
The Processorclass class of a child application must be overridden generally to inherit the Requestprocessor class, and then modify it in the <processorClass> property in the controller element of its configuration file. So when
Getrequestprocessor (Getmoduleconfig (Request)). Process (Request,response), the corresponding moduleconfig can be selected according to the request, and then according to its < The Processorclass> property selects the appropriate Requestprocessor subclass to handle the corresponding request.
Posted on 2006-11-09 10:40 jackstudio Read (12336) Comments (3) Edit favorite Categories: Java, Hibernate, struts, spring
Feedback
# re:spring has three starting modes, using Contextloaderservlet,contextloaderlistener and Contextloaderplugin. [Not logged in]
2013-07-12 12:00 | Df
SDF Reply to more comments
# re:spring has three starting modes, using Contextloaderservlet,contextloaderlistener and Contextloaderplugin. [Not logged in]
2013-07-12 12:01 | Df
Thank you for your reply! :)
Recommended purchase of cloud servers (15% rebate + max thousand bonus)
Blog Park Bo asked it news Java programmer Recruitment
Title
Name
Home
Verification Code *
Content (please do not post any political-related content) reply to more comments
# re:spring has three starting modes, using Contextloaderservlet,contextloaderlistener and Contextloaderplugin. [Not logged in]
2013-07-12 12:02 | Df
Spring has three starting modes, using Contextloaderservlet,contextloaderlistener and Contextloaderplugin.
Look at the source code of Contextloaderlistener, this is a servletcontextlistener
/**
* Initialize the root Web application context.
*/
public void contextinitialized (Servletcontextevent event) {
This.contextloader = Createcontextloader ();
This.contextLoader.initWebApplicationContext (Event.getservletcontext ());
}
/**
* Create the Contextloader to use. Can is overridden in subclasses.
* @return The new Contextloader
*/
Protected Contextloader Createcontextloader () {
return new Contextloader ();
}
Source Code of Contextloader
Public Webapplicationcontext Initwebapplicationcontext (ServletContext servletcontext)
Throws Beansexception {
Long startTime = System.currenttimemillis ();
if (logger.isinfoenabled ()) {
Logger.info ("Root webapplicationcontext:initialization started");
}
ServletContext.log ("Loading Spring root Webapplicationcontext");
try {
Determine parent for root Web application context, if any.
ApplicationContext parent = Loadparentcontext (ServletContext);
Webapplicationcontext WAC = Createwebapplicationcontext (ServletContext, parent);
Servletcontext.setattribute (
Webapplicationcontext.root_web_application_context_attribute, WAC);
if (logger.isinfoenabled ()) {
Logger.info ("Using context class [" + Wac.getclass (). GetName () +
"] for root webapplicationcontext");
}
if (logger.isdebugenabled ()) {
Logger.debug ("Published root webapplicationcontext [" + WAC +
"] as ServletContext attribute with name [" +
Webapplicationcontext.root_web_application_context_attribute + "]");
}
if (logger.isinfoenabled ()) {
Long elapsedtime = System.currenttimemillis ()-startTime;
Logger.info ("Root Webapplicationcontext:initialization completed in" + ElapsedTime + "MS");
}
return WAC;
}
catch (RuntimeException ex) {
Logger.error ("Context initialization Failed", ex);
Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, ex);
Throw ex;
}
catch (Error err) {
Logger.error ("Context initialization Failed", err);
Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, err);
throw err;
}
}
Pay attention to Webapplicationcontext.root_web_application_context_attribute, this place webapplicationcontext, To be removed from ServletContext when used
You can use Webapplicationcontextutils to get Webapplicationcontext
public static Webapplicationcontext Getwebapplicationcontext (ServletContext SC) {
Object attr = Sc.getattribute (Webapplicationcontext.root_web_application_context_attribute);
if (attr = = null) {
return null;
}
if (attr instanceof runtimeexception) {
Throw (runtimeexception) attr;
}
if (attr instanceof Error) {
Throw (Error) attr;
}
if (! ( attr instanceof Webapplicationcontext)) {
throw new IllegalStateException ("Root context attribute is not of type Webapplicationcontext:" + attr);
}
Return (Webapplicationcontext) attr;
}
The key question is how struts launches the spring, Contextloaderplugin source
Publish the context as a servlet context attribute.
String attrname = Getservletcontextattributename ();
Getservletcontext (). SetAttribute (Attrname, WAC);
Public String Getservletcontextattributename () {
return Servlet_context_prefix + getmoduleprefix ();
}
Different loaded key is different, the reason is webapplicationcontext put in the problem, but spring call will be based on the name defined in the Webapplicationcontext, the problem is found here
Configuring in Struts-config.xml
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >
<set-property property= "contextconfiglocation" value= "/web-inf/applicationcontext.xml"/>
</plug-in>
<controller>
<set-property property= "Processorclass" value= "Org.springframework.web.struts.DelegatingRequestProcessor"/ >
</controller>
The principle is this, struts can have only one actionservlet instance, but for different sub-applications can have their own requestprocessor instance each requestprocessor instance corresponding to different struts configuration files respectively.
The Processorclass class of a child application must be overridden generally to inherit the Requestprocessor class, and then modify it in the <processorClass> property in the controller element of its configuration file. So when
Getrequestprocessor (Getmoduleconfig (Request)). Process (Request,response), the corresponding moduleconfig can be selected according to the request, and then according to its < The Processorclass> property selects the appropriate Requestprocessor subclass to handle the corresponding request.
Spring Start Mode