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-issuemaster opened this issue Nov 13, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

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:

  • #16238 Provide a mechanism for adding ApplicationContextInitializer instances ("is duplicated by")
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 26, 2015

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 26, 2015

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 31, 2015

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

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
You can’t perform that action at this time.