Commit 2b9006b3 authored by Andy Wilkinson's avatar Andy Wilkinson

Accumulate config classes across register calls

Closes gh-11998
parent 5e0df39c
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
package org.springframework.boot.web.reactive.context; 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.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
...@@ -58,7 +62,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext ...@@ -58,7 +62,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext
private final ClassPathBeanDefinitionScanner scanner; private final ClassPathBeanDefinitionScanner scanner;
private Class<?>[] annotatedClasses; private final Set<Class<?>> annotatedClasses = new LinkedHashSet<>();
private String[] basePackages; private String[] basePackages;
...@@ -172,10 +176,11 @@ public class AnnotationConfigReactiveWebServerApplicationContext ...@@ -172,10 +176,11 @@ public class AnnotationConfigReactiveWebServerApplicationContext
* @see #scan(String...) * @see #scan(String...)
* @see #refresh() * @see #refresh()
*/ */
@Override
public final void register(Class<?>... annotatedClasses) { public final void register(Class<?>... annotatedClasses) {
Assert.notEmpty(annotatedClasses, Assert.notEmpty(annotatedClasses,
"At least one annotated class must be specified"); "At least one annotated class must be specified");
this.annotatedClasses = annotatedClasses; this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
} }
/** /**
...@@ -185,6 +190,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext ...@@ -185,6 +190,7 @@ public class AnnotationConfigReactiveWebServerApplicationContext
* @see #register(Class...) * @see #register(Class...)
* @see #refresh() * @see #refresh()
*/ */
@Override
public final void scan(String... basePackages) { public final void scan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified"); Assert.notEmpty(basePackages, "At least one base package must be specified");
this.basePackages = basePackages; this.basePackages = basePackages;
...@@ -202,8 +208,9 @@ public class AnnotationConfigReactiveWebServerApplicationContext ...@@ -202,8 +208,9 @@ public class AnnotationConfigReactiveWebServerApplicationContext
if (!ObjectUtils.isEmpty(this.basePackages)) { if (!ObjectUtils.isEmpty(this.basePackages)) {
this.scanner.scan(this.basePackages); this.scanner.scan(this.basePackages);
} }
if (!ObjectUtils.isEmpty(this.annotatedClasses)) { if (!this.annotatedClasses.isEmpty()) {
this.reader.register(this.annotatedClasses); this.reader.register(this.annotatedClasses
.toArray(new Class<?>[this.annotatedClasses.size()]));
} }
} }
......
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
package org.springframework.boot.web.servlet.context; 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.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
...@@ -55,7 +59,7 @@ public class AnnotationConfigServletWebServerApplicationContext ...@@ -55,7 +59,7 @@ public class AnnotationConfigServletWebServerApplicationContext
private final ClassPathBeanDefinitionScanner scanner; private final ClassPathBeanDefinitionScanner scanner;
private Class<?>[] annotatedClasses; private final Set<Class<?>> annotatedClasses = new LinkedHashSet<>();
private String[] basePackages; private String[] basePackages;
...@@ -173,7 +177,7 @@ public class AnnotationConfigServletWebServerApplicationContext ...@@ -173,7 +177,7 @@ public class AnnotationConfigServletWebServerApplicationContext
public final void register(Class<?>... annotatedClasses) { public final void register(Class<?>... annotatedClasses) {
Assert.notEmpty(annotatedClasses, Assert.notEmpty(annotatedClasses,
"At least one annotated class must be specified"); "At least one annotated class must be specified");
this.annotatedClasses = annotatedClasses; this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
} }
/** /**
...@@ -201,8 +205,9 @@ public class AnnotationConfigServletWebServerApplicationContext ...@@ -201,8 +205,9 @@ public class AnnotationConfigServletWebServerApplicationContext
if (this.basePackages != null && this.basePackages.length > 0) { if (this.basePackages != null && this.basePackages.length > 0) {
this.scanner.scan(this.basePackages); this.scanner.scan(this.basePackages);
} }
if (this.annotatedClasses != null && this.annotatedClasses.length > 0) { if (!this.annotatedClasses.isEmpty()) {
this.reader.register(this.annotatedClasses); this.reader.register(this.annotatedClasses
.toArray(new Class<?>[this.annotatedClasses.size()]));
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,6 +26,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -26,6 +26,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link AnnotationConfigReactiveWebServerApplicationContext}. * Tests for {@link AnnotationConfigReactiveWebServerApplicationContext}.
...@@ -59,6 +60,17 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests { ...@@ -59,6 +60,17 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests {
verifyContext(); 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 @Test
public void scanAndRefresh() { public void scanAndRefresh() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext(); this.context = new AnnotationConfigReactiveWebServerApplicationContext();
...@@ -85,4 +97,14 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests { ...@@ -85,4 +97,14 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests {
} }
@Configuration
public static class HttpHandlerConfiguration {
@Bean
public HttpHandler httpHandler() {
return mock(HttpHandler.class);
}
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -89,6 +89,16 @@ public class AnnotationConfigServletWebServerApplicationContextTests { ...@@ -89,6 +89,16 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
verifyContext(); 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 @Test
public void scanAndRefresh() { public void scanAndRefresh() {
this.context = new AnnotationConfigServletWebServerApplicationContext(); this.context = new AnnotationConfigServletWebServerApplicationContext();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment