Spring-Programmed Refresh/reload Applicationcontext/dispatchservlet (correct version)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.