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

spring-context-indexer doesn't support Java records #26909

Closed
sergey-morenets opened this issue May 7, 2021 · 4 comments
Closed

spring-context-indexer doesn't support Java records #26909

sergey-morenets opened this issue May 7, 2021 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sergey-morenets
Copy link

Spring version: 5.3.6
JDK: 16.0.1

It seems that spring-context-indexer library doesn't support Java records. If I declare a record as Spring bean:

@Component
public record ServiceManager() {}

then META-INF/spring.components doesn't contain entry for this type. And Spring fails to load it during startup. I looked into source code and the following blocks seem to responsible for that:

CandidateComponentsIndexer:

private static final Set<ElementKind> TYPE_KINDS =
 Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, ElementKind.INTERFACE));

IndexedStereotypesProvider:

	@Override
	public Set<String> getStereotypes(Element element) {
		Set<String> stereotypes = new LinkedHashSet<>();
		ElementKind kind = element.getKind();
		if (kind != ElementKind.CLASS && kind != ElementKind.INTERFACE) {
			return stereotypes;
		}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 7, 2021
@jhoeller jhoeller self-assigned this May 8, 2021
@jhoeller
Copy link
Contributor

jhoeller commented May 8, 2021

I wonder why you would want to declare a record as a Spring-managed bean to begin with? Our notion of a "managed bean" seems strongly bound to traditional object semantics, including instance identity and access to delegates. Are records ever a good fit there?

Now, if you're arguing that this is a mismatch between the indexer and runtime component scanning, we should indeed align the behavior there. Even if not a recommended choice of component type, consistently detecting annotated record classes is certainly better than inconsistent runtime behavior.

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 8, 2021
@jhoeller jhoeller added this to the 5.3.7 milestone May 8, 2021
@jhoeller
Copy link
Contributor

jhoeller commented May 8, 2021

I've locally refined those checks to (element.getKind().isClass() || element.getKind() == ElementKind.INTERFACE) which seems generally sensible and implicitly includes records as well as enum types (even if the latter are not a typical managed bean choice either).

@sergey-morenets
Copy link
Author

@jhoeller Thank you for the quick response.

I guess Java records is rather new feature (which was declared stable only in JDK 16) so there' no so much best practices around it.
What surprised me is that I can register Java record as Spring bean without spring-context-indexer but unable to register if I turn on spring-context-indexer.

@sergey-morenets
Copy link
Author

So I will understand if you remove Java records support at all. But it looks like inconsistent behavior now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants