Skip to content

ServletConfigAware-related exception is not thrown as expected [SPR-9855] #14488

@spring-projects-issues

Description

@spring-projects-issues

Ivan Sopov opened SPR-9855 and commented

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

	}

}

Affects: 3.1.2

Referenced from: commits 7fdb637, 03c3dc3

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bugtype: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions