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!");
      		}
      
      	}
      
      }
      

        Attachments

          Activity

            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, 34 weeks, 2 days ago