Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9610

Initialize ServletContext and ServletConfig property sources eagerly in FrameworkServlet

    Details

    • Last commented by a User:
      true

      Description

      ServletContext and ServletConfig properties are added too late in FrameworkServlet initializer. I wanted to use the Environment in an ApplicationContextInitializer including the property sources from web.xml (as advertised in StandardServletEnvironment), but StandardServletEnvironment isn't initialized until the refresh starts. The workaround is easy - just call

      WebApplicationContextUtils.initServletPropertySources(
          applicationContext.getEnvironment().getPropertySources(),
          servletContext, applicationContext.getServletConfig()
      );

      in the ApplicationContextInitializer, for example: https://github.com/cloudfoundry/uaa/blob/master/common/src/main/java/org/cloudfoundry/identity/uaa/config/YamlServletProfileInitializer.java#L58

        Issue Links

          Activity

          Hide
          cbeams Chris Beams added a comment -

          Thanks, Dave.

          commit 1070e4d5c1daec820f12ea39a4d665106d1e9539
          Author: Chris Beams <[email protected]>
          Commit: Chris Beams <[email protected]>
           
              Initialize FrameworkServlet property sources eagerly
              
              Prior to this change,
              FrameworkServlet#configureAndRefreshWebApplicationContext called
               #postProcessWebApplicationContext(wac) and #applyInitializers(wac)
              prior to #refresh, but because servlet-based property source stubs were
              not replaced until #refresh, any post-processing or initialization
              routines could not benefit from accessing the Environment to retrieve
              properties from the ServletContext or ServletConfig.
              
              The workaround to this problem is detailed in SPR-9610 - the user simply
              needed to call WebApplicationContextUtils#initServletPropertySources
              manually within their ApplicationContextInitializer (or overridden
               #postProcessWebApplicationContext method)
              
              This commit ensures that
              FrameworkServlet#configureAndRefreshWebApplicationContext calls
              WebApplicationContextUtils#initServletPropertySources eagerly, prior to
              invoking #postProcessWebApplicationContext and #applyInitializers.
              Related Javadoc has also been updated throughout to clarify the behavior
              of #initServletPropertySources, when it can be called and what the
              effects are, etc.
              
              Note also that a reproduction issue was added to demonstrate the problem
              and verify its resolution [1].
              
              [1]: https://github.com/SpringSource/spring-framework-issues/tree/master/SPR-9610
              
              Issue: SPR-9610

          Show
          cbeams Chris Beams added a comment - Thanks, Dave. commit 1070e4d5c1daec820f12ea39a4d665106d1e9539 Author: Chris Beams <[email protected]> Commit: Chris Beams <[email protected]>   Initialize FrameworkServlet property sources eagerly Prior to this change, FrameworkServlet#configureAndRefreshWebApplicationContext called #postProcessWebApplicationContext(wac) and #applyInitializers(wac) prior to #refresh, but because servlet-based property source stubs were not replaced until #refresh, any post-processing or initialization routines could not benefit from accessing the Environment to retrieve properties from the ServletContext or ServletConfig. The workaround to this problem is detailed in SPR-9610 - the user simply needed to call WebApplicationContextUtils#initServletPropertySources manually within their ApplicationContextInitializer (or overridden #postProcessWebApplicationContext method) This commit ensures that FrameworkServlet#configureAndRefreshWebApplicationContext calls WebApplicationContextUtils#initServletPropertySources eagerly, prior to invoking #postProcessWebApplicationContext and #applyInitializers. Related Javadoc has also been updated throughout to clarify the behavior of #initServletPropertySources, when it can be called and what the effects are, etc. Note also that a reproduction issue was added to demonstrate the problem and verify its resolution [1]. [1]: https://github.com/SpringSource/spring-framework-issues/tree/master/SPR-9610 Issue: SPR-9610
          Hide
          cbeams Chris Beams added a comment -

          This issue solves the problem for FrameworkServlet, but note that SPR-9439 solved the problem for ContextLoader.

          Show
          cbeams Chris Beams added a comment - This issue solves the problem for FrameworkServlet , but note that SPR-9439 solved the problem for ContextLoader .

            People

            • Assignee:
              cbeams Chris Beams
              Reporter:
              david_syer Dave Syer
              Last updater:
              Chris Beams
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 4 weeks ago

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0d
                0d
                Logged:
                Time Spent - 0.25d
                0.25d