Skip to content

Commit 221f06f

Browse files
authored
Merge pull request quarkusio#31826 from rquinio/bugfix/spring-list-jsr303
Support SpringDI List injection with @Inject
2 parents b15ca34 + 9125934 commit 221f06f

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

docs/src/main/asciidoc/spring-di.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ The following table shows how Spring DI annotations can be converted to CDI and
283283

284284
|@Autowired
285285
|@Inject
286-
|
286+
|If the type is `java.util.List`, the `io.quarkus.arc.All` qualifier is added.
287287

288288
|@Qualifier
289289
|@Named

extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java

+33-19
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,10 @@ Set<AnnotationInstance> getAnnotationsToAdd(
395395
Collections.singletonList((AnnotationValue.createStringValue("value", value)))));
396396
}
397397
}
398-
399-
// in Spring List<SomeBean> means that all instances of SomeBean should be injected
400-
if (fieldInfo.type().name().equals(DotNames.LIST)) {
401-
annotationsToAdd.add(create(
402-
QUARKUS_ALL_ANNOTATION,
403-
target,
404-
Collections.emptyList()));
405-
}
398+
addAllAnnotationOnListField(target, annotationsToAdd, fieldInfo);
399+
} else if (fieldInfo.hasAnnotation(CDI_INJECT_ANNOTATION)) {
400+
// Mix case of JSR-303 support in Spring
401+
addAllAnnotationOnListField(target, annotationsToAdd, fieldInfo);
406402
} else if (fieldInfo.hasAnnotation(SPRING_VALUE_ANNOTATION)) {
407403
final AnnotationInstance annotation = fieldInfo.annotation(SPRING_VALUE_ANNOTATION);
408404
addSpringValueAnnotations(target, annotation, true, annotationsToAdd);
@@ -437,18 +433,11 @@ Set<AnnotationInstance> getAnnotationsToAdd(
437433
CDI_INJECT_ANNOTATION,
438434
target,
439435
Collections.emptyList()));
440-
// in Spring List<SomeBean> means that all instances of SomeBean should be injected
441-
List<Type> parameters = methodInfo.parameterTypes();
442-
for (int i = 0; i < parameters.size(); i++) {
443-
Type parameter = parameters.get(i);
444-
if (parameter.name().equals(DotNames.LIST)) {
445-
annotationsToAdd.add(create(
446-
QUARKUS_ALL_ANNOTATION,
447-
MethodParameterInfo.create(methodInfo, (short) i),
448-
Collections.emptyList()));
449-
}
450-
}
436+
addAllAnnotationOnMethodListParameters(annotationsToAdd, methodInfo);
451437

438+
} else if (methodInfo.hasAnnotation(CDI_INJECT_ANNOTATION)) {
439+
// Mix case of JSR-303 support in Spring
440+
addAllAnnotationOnMethodListParameters(annotationsToAdd, methodInfo);
452441
}
453442

454443
// add method parameter conversion annotations
@@ -473,6 +462,31 @@ Set<AnnotationInstance> getAnnotationsToAdd(
473462
return annotationsToAdd;
474463
}
475464

465+
private void addAllAnnotationOnListField(AnnotationTarget target, Set<AnnotationInstance> annotationsToAdd,
466+
FieldInfo fieldInfo) {
467+
// in Spring List<SomeBean> means that all instances of SomeBean should be injected
468+
if (fieldInfo.type().name().equals(DotNames.LIST)) {
469+
annotationsToAdd.add(create(
470+
QUARKUS_ALL_ANNOTATION,
471+
target,
472+
Collections.emptyList()));
473+
}
474+
}
475+
476+
private void addAllAnnotationOnMethodListParameters(Set<AnnotationInstance> annotationsToAdd, MethodInfo methodInfo) {
477+
// in Spring List<SomeBean> means that all instances of SomeBean should be injected
478+
List<Type> parameters = methodInfo.parameterTypes();
479+
for (int i = 0; i < parameters.size(); i++) {
480+
Type parameter = parameters.get(i);
481+
if (parameter.name().equals(DotNames.LIST)) {
482+
annotationsToAdd.add(create(
483+
QUARKUS_ALL_ANNOTATION,
484+
MethodParameterInfo.create(methodInfo, (short) i),
485+
Collections.emptyList()));
486+
}
487+
}
488+
}
489+
476490
/**
477491
* Meant to be called with instances of @Component, @Service, @Repository
478492
*/

extensions/spring-di/deployment/src/test/java/io/quarkus/spring/di/deployment/ListOfBeansTest.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@ public class ListOfBeansTest {
1919
@RegisterExtension
2020
static final QuarkusUnitTest config = new QuarkusUnitTest()
2121
.withApplicationRoot((jar) -> jar
22-
.addClasses(Foo.class, ServiceAlpha.class, ServiceBravo.class, Service.class,
22+
.addClasses(Foo.class, Bar.class, ServiceAlpha.class, ServiceBravo.class, Service.class,
2323
Converter.class, ConverterAlpha.class, ConverterBravo.class));
2424

2525
@Inject
2626
Foo foo;
2727

28+
@Inject
29+
Bar bar;
30+
2831
@Test
2932
public void testInjection() {
3033
assertThat(foo.services).hasSize(2).extractingResultOf("ping").containsExactlyInAnyOrder("alpha", "bravo");
3134
assertThat(foo.converters).hasSize(2).extractingResultOf("pong").containsExactlyInAnyOrder("alpha", "bravo");
35+
36+
assertThat(bar.services).hasSize(2).extractingResultOf("ping").containsExactlyInAnyOrder("alpha", "bravo");
37+
assertThat(bar.converters).hasSize(2).extractingResultOf("pong").containsExactlyInAnyOrder("alpha", "bravo");
3238
}
3339

3440
@org.springframework.stereotype.Service
@@ -43,6 +49,23 @@ public static class Foo {
4349
Foo(List<Converter> converters) {
4450
this.converters = converters;
4551
}
52+
}
53+
54+
/**
55+
* Test Spring with JSR-303 support
56+
*/
57+
@org.springframework.stereotype.Service
58+
public static class Bar {
59+
60+
@Inject
61+
List<Service> services;
62+
63+
final List<Converter> converters;
64+
65+
@Inject
66+
Bar(List<Converter> converters) {
67+
this.converters = converters;
68+
}
4669

4770
}
4871

0 commit comments

Comments
 (0)