Skip to content

Support org.springframework.context.ApplicationContextInitializer usage programmatically (in Servlet 3.0+ environments) [SPR-12430] #17036

@spring-projects-issues

Description

@spring-projects-issues

Hendy Irawan opened SPR-12430 and commented

org.springframework.context.ApplicationContextInitializer is only usable from context-param or init-param.

It should also be usable programmatically, e.g. from WebApplicationInitializer implementation:

@Override
protected WebApplicationContext createRootApplicationContext() {
	final ConfigurableWebApplicationContext webAppCtx = (ConfigurableWebApplicationContext) super.createRootApplicationContext();
	// this throws NullPointerException due to ServletContext not yet assigned to webAppCtx
	new AppInitializer().initialize(webAppCtx);
	// Ideally
	webAppCtx.setInitializers(webAppCtx);
	return webAppCtx;
}

Workaround:

@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	// Workaround
	servletContext.setInitParameter("contextInitializerClasses", AppInitializer.class.getName());
	super.onStartup(servletContext);
	servletContext.addListener(RequestContextListener.class);
}

Affects: 4.1.2

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions