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

ServletConfigAware-related exception is not thrown as expected

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.1.2
    • Fix Version/s: 3.1.3, 3.2 RC1
    • Component/s: [Documentation], Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      From the ServletConfigAware javadoc:

      Only satisfied if actually running within a Servlet-specific
      WebApplicationContext. If this callback interface is encountered
      elsewhere, an exception will be thrown on bean creation

      But I have not been able to reproduce exception throwing and I have not found it in the source. E.g. this code illustrates that ServletConfigAware interface does not work and exception is not thrown:

      package com.sopovs.moradanen;
       
      import javax.servlet.ServletConfig;
       
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.mock.web.MockServletConfig;
      import org.springframework.stereotype.Component;
      import org.springframework.web.context.ServletConfigAware;
      import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
       
      @Configuration
      public class ServletConfigAwareTest {
       
      	public static void main(String[] args) {
      		System.out.println(new AnnotationConfigApplicationContext(
      				"com.sopovs.moradanen").getBean(Foo.class).hashCode());
       
      		AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
      		webContext.scan("com.sopovs.moradanen");
      		webContext.setServletConfig(new MockServletConfig());
      		webContext.afterPropertiesSet();
      		System.out.println(webContext.getBean(Foo.class).hashCode());
       
      		// where is my exception?
      	}
       
      	@Component
      	public static class Foo implements ServletConfigAware {
       
      		public void setServletConfig(ServletConfig servletConfig) {
      			System.out.println("Bingo!");
      		}
       
      	}
       
      }

        Activity

        Hide
        cbeams Chris Beams added a comment -

        Ivan Sopov, thanks for the report. I've done a quick verification based on your sample that indeed an exception is not thrown when you might expect it to be based on the Javadoc for ServletConfigAware. At this point, the correct solution may be to update the documentation, as there may be existing applications that depend on the fact that no exception is thrown, whether intentionally or by accident. i.e. it could be a backward compatibility issue to begin throwing such an exception now.

        [~philwebb], I've scheduled this for the General Backlog, but feel free to pick it up if you'd like to take a little deeper look and determine whether a doc fix is indeed appropriate.

        Show
        cbeams Chris Beams added a comment - Ivan Sopov , thanks for the report. I've done a quick verification based on your sample that indeed an exception is not thrown when you might expect it to be based on the Javadoc for ServletConfigAware . At this point, the correct solution may be to update the documentation, as there may be existing applications that depend on the fact that no exception is thrown, whether intentionally or by accident. i.e. it could be a backward compatibility issue to begin throwing such an exception now. [~philwebb] , I've scheduled this for the General Backlog, but feel free to pick it up if you'd like to take a little deeper look and determine whether a doc fix is indeed appropriate.
        Hide
        isopov Ivan Sopov added a comment -

        Yes, I think removing this statement from javadoc is perfectly fine. However warning or error in logs may be useful in such case.

        Show
        isopov Ivan Sopov added a comment - Yes, I think removing this statement from javadoc is perfectly fine. However warning or error in logs may be useful in such case.
        Hide
        juergen.hoeller Juergen Hoeller added a comment -

        Indeed, I'm simply removing that statement from the documentation there, making clear that no ServletConfig will be set when not running within a Servlet-specific WebApplicationContext. This is the intended behavior, actually, since we're using ServletConfigAware ourselves to check for the optional introspection of Servlet-specific init parameters...

        Juergen

        Show
        juergen.hoeller Juergen Hoeller added a comment - Indeed, I'm simply removing that statement from the documentation there, making clear that no ServletConfig will be set when not running within a Servlet-specific WebApplicationContext. This is the intended behavior, actually, since we're using ServletConfigAware ourselves to check for the optional introspection of Servlet-specific init parameters... Juergen

          People

          • Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            isopov Ivan Sopov
            Last updater:
            Chris Beams
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

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