Skip to content

Allow SpringApplication to create its application context without requiring reflection #22322

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

Closed
dsyer opened this issue Jul 14, 2020 · 5 comments
Assignees
Labels
theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Milestone

Comments

@dsyer
Copy link
Member

dsyer commented Jul 14, 2020

I found I needed to access the applicationContextClass reflectively in order to reason about whether I needed to change the default. This was specifically when trying to avoid annotation-based @Configuration processing, but I guess there might be other reasons to want to do it. So adding a public accessor for SpringApplication.applicationContextClass would get me where I need to go.

Example code in Spring Init:

WebApplicationType type = getWebApplicationType(application, prepared.getEnvironment()); // assumptions made here			
Class<?> contextType = getApplicationContextType(application); // reflective access here
if (type == WebApplicationType.NONE) {
	if (contextType == AnnotationConfigApplicationContext.class || contextType == null) {
		application.setApplicationContextClass(GenericApplicationContext.class);
	}
} else if (type == WebApplicationType.REACTIVE) {
	if (contextType == AnnotationConfigReactiveWebApplicationContext.class || contextType == null) {
		application.setApplicationContextClass(ReactiveWebServerApplicationContext.class);
	}
} else if (type == WebApplicationType.SERVLET) {
	if (contextType == AnnotationConfigServletWebServerApplicationContext.class || contextType == null) {
		application.setApplicationContextClass(ServletWebServerApplicationContext.class);
	}
}

Alternatively we could encapsulate the concern of mapping a WebApplicationType to a default ApplicationContext. Even better would be to optionally avoid reflective instantiation of the ApplicationContext altogether.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 14, 2020
@wilkinsona wilkinsona self-assigned this Jul 14, 2020
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 14, 2020
@wilkinsona wilkinsona added this to the 2.4.x milestone Jul 14, 2020
@wilkinsona
Copy link
Member

@dsyer I've got a prototype for this. Could you please take a look and let us know if it meets your needs?

@dsyer
Copy link
Member Author

dsyer commented Jul 14, 2020

The API works. It would be nice to have access to the setter in SpringApplicationBuilder.

@wilkinsona
Copy link
Member

wilkinsona commented Jul 14, 2020

Thanks. I've force-pushed an update to the builder. I'm also wondering if we should deprecate the class-based methods. The new approach provides a superset of the capabilities of the class-based approach so it's not clear to me that we need both.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Jul 14, 2020
@dsyer
Copy link
Member Author

dsyer commented Jul 14, 2020

Kind of would be nice. The only problem with deprecating the old method is that you need it to support configuration via spring.main.application-context-class.

@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Jul 15, 2020
@wilkinsona
Copy link
Member

wilkinsona commented Jul 16, 2020

We're going to deprecate the class-based configuration in favour of the factory.

you need it to support configuration via spring.main.application-context-class

That's not an officially supported property (hence there being no metadata for it) so anyone using it (and ignoring their IDE's warning) will have to find an alternative once the deprecated setter's been removed (Boot 2.6 at the earliest).

@wilkinsona wilkinsona changed the title Refactor SpringApplication to make ApplicationContext creation optionally non-reflective Allow SpringApplication to create its application context without requiring reflection Jul 16, 2020
@snicoll snicoll modified the milestones: 2.4.x, 2.4.0-M2 Jul 16, 2020
@mbhave mbhave added the theme: aot An issue related to Ahead-of-time processing label Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants