Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Nov 13, 2014 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 13, 2014

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:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We got a setContextInitializers method on FrameworkServlet, so this can be programmatically configured there already. As a consequence, I wouldn't put a setInitializers method onto ConfigurableWebApplicationContext itself but rather onto ContextLoader, analogous to the arrangement in FrameworkServlet, processed at the very same time as "contextInitializerClasses".

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Following up on the above, I've added a setContextInitializers method to ContextLoader, also exposed on the ContextLoaderListener subclass. I'm also considering first-class support in AbstractContextLoaderInitializer since the ContextLoaderListener isn't currently exposed there. Taking AbstractDispatcherServletInitializer into account as well, it'd be nice to provide support for 'global' initializers as well as context-local initializers, analogous to the current context-param variants.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This is ready now and will be available in the next 4.2 snapshot.

AbstractContextLoaderInitializer has a protected getRootApplicationContextInitializers method now, and AbstractDispatcherServletInitializer a corresponding getServletApplicationContextInitializers method. Since it's easy enough to expose the same initializer to both contexts, I've left it at that, not adding an extra method for global initializers. Re-applying global initializers is also a bit harder here since it's about initializer instances (which are supposed to be independent per context) instead of the initializer classes approach at the context-param level.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Apr 1, 2015

cemo koc commented

Juergen, this issue seems a duplicate issue this one #16238. Please resolve my issue as well.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants