In some cases, especially when developing the application framework, it is not possible or difficult to restart Tomcat or reload applications for some reason, but the configuration needs to be dynamic, and this is usually expected through reload spring ApplicationContext the way to reload the configuration, such as the dynamic configuration of the data source.
1. config listener Contextloaderlistener in Web. xml
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener
</listener-class>
</listener>
This step is not configured to cause Webapplicationcontextutils.getwebapplicationcontext to be empty because it is the binding relationship between the listener completion context and the servlet.
2.
Webapplicationcontext context = Webapplicationcontextutils
. Getwebapplicationcontext (Request.getsession ()
. Getservletcontext ());
if (context.getparent () = null) {
((Abstractrefreshableapplicationcontext) context.getparent ())
. Refresh ();
}
((abstractrefreshableapplicationcontext) context). Refresh ();
======== The 2nd step above is only correct 1/3, to be completely correct, please refer to the following:
Webapplicationcontext context = Webapplicationcontextutils.getwebapplicationcontext (Request.getsession (). Getservletcontext (), "Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC"); --SPRINGMVC is the name of the servlet in Web. XML, the correct order is to get dispatchservet corresponding to the context, and then get root, refresh is first root, then Dispatchservlet.
if (context.getparent () = null) {
((Abstractrefreshableapplicationcontext) context.getparent ()). Refresh (); --
((abstractrefreshableapplicationcontext) context). Refresh ();
Reload and open the data source, and do whatever you want to prevent the first access from throwing an exception
Metadatadao.queryappname ();
============ memory is not good, by the way record:
Get dispatchservlet corresponding to Applicationcontext,webapplicationcontextutils.getwebapplicationcontext (Request.getSession ( ). Getservletcontext (), "Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
For Root ApplicationContext, the following three types are available:
Webapplicationcontextutils.getwebapplicationcontext (Request.getsession (). Getservletcontext ());
Webapplicationcontextutils.getwebapplicationcontext (Request.getsession (). Getservletcontext (), or G.springframework.web.context.webapplicationcontext.root);
Webapplicationcontextutils.getwebapplicationcontext (Request.getsession (). Getservletcontext (), " Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC "). GetParent ();
Spring-Programmed Refresh/reload Applicationcontext/dispatchservlet (correct version)