Reinstate qualifier support for JSR-330 @⁠javax.inject.Named

This commit revises QualifierAnnotationAutowireCandidateResolver to
reinstate "qualifier" support for the legacy JSR-330
@⁠javax.inject.Named annotation.

See gh-31090
Closes gh-33345
This commit is contained in:
Sam Brannen
2024-08-08 12:25:31 +03:00
parent f9d2641fd5
commit bcffa15c7d
5 changed files with 155 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,16 +24,24 @@ import org.springframework.aot.hint.TypeReference;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeHintsRegistrar} for Jakarta annotations.
* {@link RuntimeHintsRegistrar} for Jakarta annotations and their pre-Jakarta equivalents.
*
* @author Brian Clozel
* @author Sam Brannen
*/
class JakartaAnnotationsRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
Stream.of("jakarta.inject.Inject", "jakarta.inject.Provider", "jakarta.inject.Qualifier").forEach(typeName ->
hints.reflection().registerType(TypeReference.of(typeName)));
// javax.inject.Provider is omitted from the list, since we do not currently load
// it via reflection.
Stream.of(
"jakarta.inject.Inject",
"jakarta.inject.Provider",
"jakarta.inject.Qualifier",
"javax.inject.Inject",
"javax.inject.Qualifier"
).forEach(typeName -> hints.reflection().registerType(TypeReference.of(typeName)));
}
}

View File

@@ -47,11 +47,13 @@ import org.springframework.util.ObjectUtils;
* against {@link Qualifier qualifier annotations} on the field or parameter to be autowired.
* Also supports suggested expression values through a {@link Value value} annotation.
*
* <p>Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation, if available.
* <p>Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation (as well as its
* pre-Jakarta {@code javax.inject.Qualifier} equivalent), if available.
*
* @author Mark Fisher
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Sam Brannen
* @since 2.5
* @see AutowireCandidateQualifier
* @see Qualifier
@@ -65,9 +67,10 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
/**
* Create a new QualifierAnnotationAutowireCandidateResolver
* for Spring's standard {@link Qualifier} annotation.
* <p>Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation, if available.
* Create a new {@code QualifierAnnotationAutowireCandidateResolver} for Spring's
* standard {@link Qualifier} annotation.
* <p>Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation (as well as
* its pre-Jakarta {@code javax.inject.Qualifier} equivalent), if available.
*/
@SuppressWarnings("unchecked")
public QualifierAnnotationAutowireCandidateResolver() {
@@ -76,6 +79,13 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa
this.qualifierTypes.add((Class<? extends Annotation>) ClassUtils.forName("jakarta.inject.Qualifier",
QualifierAnnotationAutowireCandidateResolver.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
// JSR-330 API (as included in Jakarta EE) not available - simply skip.
}
try {
this.qualifierTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Qualifier",
QualifierAnnotationAutowireCandidateResolver.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link JakartaAnnotationsRuntimeHints}.
*
* @author Brian Clozel
* @author Sam Brannen
*/
class JakartaAnnotationsRuntimeHintsTests {
@@ -62,4 +63,14 @@ class JakartaAnnotationsRuntimeHintsTests {
assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)).accepts(this.hints);
}
@Test // gh-33345
void javaxInjectAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(javax.inject.Inject.class)).accepts(this.hints);
}
@Test // gh-33345
void javaxQualifierAnnotationHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(javax.inject.Qualifier.class)).accepts(this.hints);
}
}