Skip to content

Commit

Permalink
Spring' @scope#scopeName is now taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and gsmet committed Nov 16, 2020
1 parent c9995bb commit c71a7e7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {

}
Expand All @@ -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 {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,7 +23,11 @@ public class InjectedSpringBeansResource {
@Inject
SessionBean sessionBean;
@Inject
CustomPrototypeBean anotherRequestBean;
CustomPrototypeBean customPrototypeBean;
@Inject
PrototypeBean prototypeBean;
@Inject
PrototypeBean anotherPrototypeBean;
@Inject
NamedBean namedBean;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit c71a7e7

Please sign in to comment.