Accumulate config classes across register calls

Closes gh-11998
This commit is contained in:
Andy Wilkinson 2018-02-12 09:59:31 +00:00
parent 5e0df39c66
commit 2b9006b3fd
4 changed files with 54 additions and 10 deletions

View File

@ -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<Class<?>> 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()]));
}
}

View File

@ -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<Class<?>> 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()]));
}
}

View File

@ -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);
}
}
}

View File

@ -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();