From c71a7e783d3a3b7a8de6a6b6f6d6cca2126e2564 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 21 Sep 2020 15:23:40 +0300 Subject: [PATCH] Spring' @Scope#scopeName is now taken into account Fixes: #12233 --- .../di/deployment/SpringDIProcessor.java | 6 ++++++ .../quarkus/it/spring/AppConfiguration.java | 20 +++++++++++++++++++ .../spring/InjectedSpringBeansResource.java | 13 +++++++++++- .../InjectedSpringBeansResourceTest.java | 7 +++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java b/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java index 467e153e544ce..ac1cab78a2852 100644 --- a/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java +++ b/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java @@ -208,10 +208,16 @@ private DotName getScope(final AnnotationTarget target) { if (target.kind() == AnnotationTarget.Kind.CLASS) { if (target.asClass().classAnnotation(SPRING_SCOPE_ANNOTATION) != null) { value = target.asClass().classAnnotation(SPRING_SCOPE_ANNOTATION).value(); + if ((value == null) || value.asString().isEmpty()) { + value = target.asClass().classAnnotation(SPRING_SCOPE_ANNOTATION).value("scopeName"); + } } } else if (target.kind() == AnnotationTarget.Kind.METHOD) { if (target.asMethod().hasAnnotation(SPRING_SCOPE_ANNOTATION)) { value = target.asMethod().annotation(SPRING_SCOPE_ANNOTATION).value(); + if ((value == null) || value.asString().isEmpty()) { + value = target.asMethod().annotation(SPRING_SCOPE_ANNOTATION).value("scopeName"); + } } } if (value != null) { diff --git a/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/AppConfiguration.java b/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/AppConfiguration.java index c2c0d08be8ba9..8931467b62f57 100644 --- a/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/AppConfiguration.java +++ b/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/AppConfiguration.java @@ -1,11 +1,14 @@ package io.quarkus.it.spring; +import java.util.concurrent.atomic.AtomicInteger; + import javax.inject.Named; import javax.inject.Singleton; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; import org.springframework.web.context.annotation.RequestScope; @Configuration @@ -40,6 +43,14 @@ public CustomPrototypeBean beanWithCustomPrototype() { return new CustomPrototypeBean(); } + private AtomicInteger prototypeBeanCounter = new AtomicInteger(0); + + @Bean + @Scope(scopeName = "prototype") + public PrototypeBean prototypeBean() { + return new PrototypeBean(prototypeBeanCounter.getAndIncrement()); + } + private static class SingletonBean { } @@ -48,6 +59,15 @@ private static class AnotherRequestBean { } + public static class PrototypeBean { + + public final int index; + + public PrototypeBean(int index) { + this.index = index; + } + } + public static class CustomPrototypeBean { } diff --git a/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/InjectedSpringBeansResource.java b/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/InjectedSpringBeansResource.java index 3dd0a8ffd287b..0ca5c38cfce1c 100644 --- a/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/InjectedSpringBeansResource.java +++ b/integration-tests/spring-di/src/main/java/io/quarkus/it/spring/InjectedSpringBeansResource.java @@ -11,6 +11,7 @@ import io.quarkus.it.spring.AppConfiguration.CustomPrototypeBean; import io.quarkus.it.spring.AppConfiguration.NamedBean; +import io.quarkus.it.spring.AppConfiguration.PrototypeBean; @Path("/") public class InjectedSpringBeansResource { @@ -22,7 +23,11 @@ public class InjectedSpringBeansResource { @Inject SessionBean sessionBean; @Inject - CustomPrototypeBean anotherRequestBean; + CustomPrototypeBean customPrototypeBean; + @Inject + PrototypeBean prototypeBean; + @Inject + PrototypeBean anotherPrototypeBean; @Inject NamedBean namedBean; @@ -51,4 +56,10 @@ public int getSessionValue() { public void invalidate(final @Context HttpServletRequest req) { req.getSession().invalidate(); } + + @GET + @Path("prototype") + public String prototype() { + return prototypeBean.index + "/" + anotherPrototypeBean.index; + } } diff --git a/integration-tests/spring-di/src/test/java/io/quarkus/it/spring/InjectedSpringBeansResourceTest.java b/integration-tests/spring-di/src/test/java/io/quarkus/it/spring/InjectedSpringBeansResourceTest.java index 690e58ed961ec..52ec3006f33f3 100644 --- a/integration-tests/spring-di/src/test/java/io/quarkus/it/spring/InjectedSpringBeansResourceTest.java +++ b/integration-tests/spring-di/src/test/java/io/quarkus/it/spring/InjectedSpringBeansResourceTest.java @@ -58,4 +58,11 @@ public void testSessionScope() { .statusCode(200) .body(Matchers.is("0")); } + + @Test + public void testPrototypeScope() { + RestAssured.when().get("/spring-test/prototype").then() + .body(Matchers.containsString("0")) + .body(Matchers.containsString("1")); + } }