From 2b9006b3fd940447f87b5c85515b21a792eef6e6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 12 Feb 2018 09:59:31 +0000 Subject: [PATCH] Accumulate config classes across register calls Closes gh-11998 --- ...igReactiveWebServerApplicationContext.java | 15 ++++++++---- ...figServletWebServerApplicationContext.java | 13 ++++++---- ...ctiveWebServerApplicationContextTests.java | 24 ++++++++++++++++++- ...rvletWebServerApplicationContextTests.java | 12 +++++++++- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContext.java index 31d49296b0..ed8573f952 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContext.java @@ -16,6 +16,10 @@ package org.springframework.boot.web.reactive.context; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; + import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -58,7 +62,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext private final ClassPathBeanDefinitionScanner scanner; - private Class[] annotatedClasses; + private final Set> annotatedClasses = new LinkedHashSet<>(); private String[] basePackages; @@ -172,10 +176,11 @@ public class AnnotationConfigReactiveWebServerApplicationContext * @see #scan(String...) * @see #refresh() */ + @Override public final void register(Class... annotatedClasses) { Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified"); - this.annotatedClasses = annotatedClasses; + this.annotatedClasses.addAll(Arrays.asList(annotatedClasses)); } /** @@ -185,6 +190,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext * @see #register(Class...) * @see #refresh() */ + @Override public final void scan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); this.basePackages = basePackages; @@ -202,8 +208,9 @@ public class AnnotationConfigReactiveWebServerApplicationContext if (!ObjectUtils.isEmpty(this.basePackages)) { this.scanner.scan(this.basePackages); } - if (!ObjectUtils.isEmpty(this.annotatedClasses)) { - this.reader.register(this.annotatedClasses); + if (!this.annotatedClasses.isEmpty()) { + this.reader.register(this.annotatedClasses + .toArray(new Class[this.annotatedClasses.size()])); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContext.java index 5af5e5c079..8f4252a405 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContext.java @@ -16,6 +16,10 @@ package org.springframework.boot.web.servlet.context; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; + import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -55,7 +59,7 @@ public class AnnotationConfigServletWebServerApplicationContext private final ClassPathBeanDefinitionScanner scanner; - private Class[] annotatedClasses; + private final Set> annotatedClasses = new LinkedHashSet<>(); private String[] basePackages; @@ -173,7 +177,7 @@ public class AnnotationConfigServletWebServerApplicationContext public final void register(Class... annotatedClasses) { Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified"); - this.annotatedClasses = annotatedClasses; + this.annotatedClasses.addAll(Arrays.asList(annotatedClasses)); } /** @@ -201,8 +205,9 @@ public class AnnotationConfigServletWebServerApplicationContext if (this.basePackages != null && this.basePackages.length > 0) { this.scanner.scan(this.basePackages); } - if (this.annotatedClasses != null && this.annotatedClasses.length > 0) { - this.reader.register(this.annotatedClasses); + if (!this.annotatedClasses.isEmpty()) { + this.reader.register(this.annotatedClasses + .toArray(new Class[this.annotatedClasses.size()])); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContextTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContextTests.java index a03fa76192..3eef76f068 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContextTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -26,6 +26,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.server.reactive.HttpHandler; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; /** * Tests for {@link AnnotationConfigReactiveWebServerApplicationContext}. @@ -59,6 +60,17 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests { verifyContext(); } + @Test + public void multipleRegistersAndRefresh() { + this.context = new AnnotationConfigReactiveWebServerApplicationContext(); + this.context.register(WebServerConfiguration.class); + this.context.register(HttpHandlerConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBeansOfType(WebServerConfiguration.class)).hasSize(1); + assertThat(this.context.getBeansOfType(HttpHandlerConfiguration.class)) + .hasSize(1); + } + @Test public void scanAndRefresh() { this.context = new AnnotationConfigReactiveWebServerApplicationContext(); @@ -85,4 +97,14 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests { } + @Configuration + public static class HttpHandlerConfiguration { + + @Bean + public HttpHandler httpHandler() { + return mock(HttpHandler.class); + } + + } + } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContextTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContextTests.java index 56e95b841f..e8a0a0bcef 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContextTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -89,6 +89,16 @@ public class AnnotationConfigServletWebServerApplicationContextTests { verifyContext(); } + @Test + public void multipleRegistersAndRefresh() { + this.context = new AnnotationConfigServletWebServerApplicationContext(); + this.context.register(WebServerConfiguration.class); + this.context.register(ServletContextAwareConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBeansOfType(Servlet.class)).hasSize(1); + assertThat(this.context.getBeansOfType(ServletWebServerFactory.class)).hasSize(1); + } + @Test public void scanAndRefresh() { this.context = new AnnotationConfigServletWebServerApplicationContext();