Commit 57cad53e authored by Stephane Nicoll's avatar Stephane Nicoll

Adapt to latest API change

parent 3c21ade4
......@@ -16,11 +16,9 @@
package org.springframework.boot.web.reactive.context;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
......@@ -67,8 +65,6 @@ public class AnnotationConfigReactiveWebApplicationContext
private final Set<String> basePackages = new LinkedHashSet<>();
private final Set<BeanRegistration> registeredBeans = new LinkedHashSet<>();
@Override
protected ConfigurableEnvironment createEnvironment() {
return new StandardReactiveWebEnvironment();
......@@ -143,23 +139,6 @@ public class AnnotationConfigReactiveWebApplicationContext
this.annotatedClasses.addAll(Arrays.asList(annotatedClasses));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, qualifiers));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans
.add(new BeanRegistration(annotatedClass, supplier, qualifiers));
}
/**
* Perform a scan within the specified base packages.
* <p>
......@@ -242,9 +221,6 @@ public class AnnotationConfigReactiveWebApplicationContext
if (!this.basePackages.isEmpty()) {
scanBasePackages(scanner);
}
if (!this.registeredBeans.isEmpty()) {
registerBeans(reader);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
registerConfigLocations(reader, scanner, configLocations);
......@@ -269,16 +245,6 @@ public class AnnotationConfigReactiveWebApplicationContext
scanner.scan(StringUtils.toStringArray(this.basePackages));
}
private void registerBeans(AnnotatedBeanDefinitionReader reader) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Registering supplied beans: ["
+ StringUtils.collectionToCommaDelimitedString(this.registeredBeans)
+ "]");
}
this.registeredBeans.forEach((reg) -> reader.registerBean(reg.getAnnotatedClass(),
reg.getSupplier(), reg.getQualifiers()));
}
private void registerConfigLocations(AnnotatedBeanDefinitionReader reader,
ClassPathBeanDefinitionScanner scanner, String[] configLocations)
throws LinkageError {
......@@ -357,45 +323,4 @@ public class AnnotationConfigReactiveWebApplicationContext
return new FilteredReactiveWebContextResource(path);
}
/**
* Holder for a programmatic bean registration.
*
* @see #registerBean(Class, Class[])
* @see #registerBean(Class, Supplier, Class[])
*/
private static class BeanRegistration {
private final Class<?> annotatedClass;
private final Supplier<?> supplier;
private final Class<? extends Annotation>[] qualifiers;
BeanRegistration(Class<?> annotatedClass, Supplier<?> supplier,
Class<? extends Annotation>[] qualifiers) {
this.annotatedClass = annotatedClass;
this.supplier = supplier;
this.qualifiers = qualifiers;
}
public Class<?> getAnnotatedClass() {
return this.annotatedClass;
}
@SuppressWarnings("rawtypes")
public Supplier getSupplier() {
return this.supplier;
}
public Class<? extends Annotation>[] getQualifiers() {
return this.qualifiers;
}
@Override
public String toString() {
return this.annotatedClass.getName();
}
}
}
......@@ -16,11 +16,9 @@
package org.springframework.boot.web.reactive.context;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanNameGenerator;
......@@ -37,7 +35,6 @@ import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* {@link ReactiveWebServerApplicationContext} that accepts annotated classes as input -
......@@ -70,8 +67,6 @@ public class AnnotationConfigReactiveWebServerApplicationContext
private String[] basePackages;
private final Set<BeanRegistration> registeredBeans = new LinkedHashSet<>();
/**
* Create a new {@link AnnotationConfigReactiveWebServerApplicationContext} that needs
* to be populated through {@link #register} calls and then manually
......@@ -202,45 +197,6 @@ public class AnnotationConfigReactiveWebServerApplicationContext
this.basePackages = basePackages;
}
/**
* Register a bean from the given bean class.
* @param annotatedClass the class of the bean
* @param <T> the type of the bean
* @since 2.2.0
*/
public final <T> void registerBean(Class<T> annotatedClass) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, null));
}
/**
* Register a bean from the given bean class, using the given supplier for obtaining a
* new instance (typically declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param <T> the type of the bean
* @since 2.2.0
*/
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, supplier, null));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, qualifiers));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans
.add(new BeanRegistration(annotatedClass, supplier, qualifiers));
}
@Override
protected void prepareRefresh() {
this.scanner.clearCache();
......@@ -256,60 +212,6 @@ public class AnnotationConfigReactiveWebServerApplicationContext
if (!this.annotatedClasses.isEmpty()) {
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
if (!this.registeredBeans.isEmpty()) {
registerBeans(this.reader);
}
}
private void registerBeans(AnnotatedBeanDefinitionReader reader) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Registering supplied beans: ["
+ StringUtils.collectionToCommaDelimitedString(this.registeredBeans)
+ "]");
}
this.registeredBeans.forEach((reg) -> reader.registerBean(reg.getAnnotatedClass(),
reg.getSupplier(), reg.getQualifiers()));
}
/**
* Holder for a programmatic bean registration.
*
* @see #registerBean(Class, Class[])
* @see #registerBean(Class, Supplier, Class[])
*/
private static class BeanRegistration {
private final Class<?> annotatedClass;
private final Supplier<?> supplier;
private final Class<? extends Annotation>[] qualifiers;
BeanRegistration(Class<?> annotatedClass, Supplier<?> supplier,
Class<? extends Annotation>[] qualifiers) {
this.annotatedClass = annotatedClass;
this.supplier = supplier;
this.qualifiers = qualifiers;
}
public Class<?> getAnnotatedClass() {
return this.annotatedClass;
}
@SuppressWarnings("rawtypes")
public Supplier getSupplier() {
return this.supplier;
}
public Class<? extends Annotation>[] getQualifiers() {
return this.qualifiers;
}
@Override
public String toString() {
return this.annotatedClass.getName();
}
}
}
......@@ -16,14 +16,9 @@
package org.springframework.boot.web.servlet.context;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanNameGenerator;
......@@ -38,7 +33,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
......@@ -62,8 +56,6 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
public class AnnotationConfigServletWebServerApplicationContext
extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {
private final Log logger = LogFactory.getLog(getClass());
private final AnnotatedBeanDefinitionReader reader;
private final ClassPathBeanDefinitionScanner scanner;
......@@ -72,8 +64,6 @@ public class AnnotationConfigServletWebServerApplicationContext
private String[] basePackages;
private final Set<BeanRegistration> registeredBeans = new LinkedHashSet<>();
/**
* Create a new {@link AnnotationConfigServletWebServerApplicationContext} that needs
* to be populated through {@link #register} calls and then manually
......@@ -204,45 +194,6 @@ public class AnnotationConfigServletWebServerApplicationContext
this.basePackages = basePackages;
}
/**
* Register a bean from the given bean class.
* @param annotatedClass the class of the bean
* @param <T> the type of the bean
* @since 2.2.0
*/
public final <T> void registerBean(Class<T> annotatedClass) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, null));
}
/**
* Register a bean from the given bean class, using the given supplier for obtaining a
* new instance (typically declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param <T> the type of the bean
* @since 2.2.0
*/
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, supplier, null));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, qualifiers));
}
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier,
Class<? extends Annotation>... qualifiers) {
this.registeredBeans
.add(new BeanRegistration(annotatedClass, supplier, qualifiers));
}
@Override
protected void prepareRefresh() {
this.scanner.clearCache();
......@@ -258,60 +209,6 @@ public class AnnotationConfigServletWebServerApplicationContext
if (!this.annotatedClasses.isEmpty()) {
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
if (!this.registeredBeans.isEmpty()) {
registerBeans(this.reader);
}
}
private void registerBeans(AnnotatedBeanDefinitionReader reader) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Registering supplied beans: ["
+ StringUtils.collectionToCommaDelimitedString(this.registeredBeans)
+ "]");
}
this.registeredBeans.forEach((reg) -> reader.registerBean(reg.getAnnotatedClass(),
reg.getSupplier(), reg.getQualifiers()));
}
/**
* Holder for a programmatic bean registration.
*
* @see #registerBean(Class, Class[])
* @see #registerBean(Class, Supplier, Class[])
*/
private static class BeanRegistration {
private final Class<?> annotatedClass;
private final Supplier<?> supplier;
private final Class<? extends Annotation>[] qualifiers;
BeanRegistration(Class<?> annotatedClass, Supplier<?> supplier,
Class<? extends Annotation>[] qualifiers) {
this.annotatedClass = annotatedClass;
this.supplier = supplier;
this.qualifiers = qualifiers;
}
public Class<?> getAnnotatedClass() {
return this.annotatedClass;
}
@SuppressWarnings("rawtypes")
public Supplier getSupplier() {
return this.supplier;
}
public Class<? extends Annotation>[] getQualifiers() {
return this.qualifiers;
}
@Override
public String toString() {
return this.annotatedClass.getName();
}
}
}
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.reactive.context;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.annotation.Lazy;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link AnnotationConfigReactiveWebApplicationContext}
*
* @author Stephane Nicoll
*/
public class AnnotationConfigReactiveWebApplicationContextTests {
private AnnotationConfigReactiveWebApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void registerBean() {
this.context = new AnnotationConfigReactiveWebApplicationContext();
this.context.registerBean(TestBean.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebApplicationContextTests.TestBean")).isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithLazy() {
this.context = new AnnotationConfigReactiveWebApplicationContext();
this.context.registerBean(TestBean.class, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebApplicationContextTests.TestBean")).isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplier() {
this.context = new AnnotationConfigReactiveWebApplicationContext();
this.context.registerBean(TestBean.class, TestBean::new);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebApplicationContextTests.TestBean")).isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplierAndLazy() {
this.context = new AnnotationConfigReactiveWebApplicationContext();
this.context.registerBean(TestBean.class, TestBean::new, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebApplicationContextTests.TestBean")).isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
private static class TestBean {
}
}
......@@ -16,7 +16,6 @@
package org.springframework.boot.web.reactive.context;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.ServerManager;
......@@ -26,7 +25,6 @@ import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
......@@ -44,13 +42,6 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests {
private AnnotationConfigReactiveWebServerApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void createFromScan() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext(
......@@ -102,54 +93,6 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests {
verifyContext();
}
@Test
public void registerBean() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext();
this.context.register(ExampleReactiveWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebServerApplicationContextTests.TestBean"))
.isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithLazy() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext();
this.context.register(ExampleReactiveWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebServerApplicationContextTests.TestBean"))
.isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplier() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext();
this.context.register(ExampleReactiveWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, TestBean::new);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebServerApplicationContextTests.TestBean"))
.isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplierAndLazy() {
this.context = new AnnotationConfigReactiveWebServerApplicationContext();
this.context.register(ExampleReactiveWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, TestBean::new, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigReactiveWebServerApplicationContextTests.TestBean"))
.isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
private void verifyContext() {
MockReactiveWebServerFactory factory = this.context
.getBean(MockReactiveWebServerFactory.class);
......@@ -231,8 +174,4 @@ public class AnnotationConfigReactiveWebServerApplicationContextTests {
}
private static class TestBean {
}
}
......@@ -32,7 +32,6 @@ import org.springframework.boot.web.servlet.server.MockServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
......@@ -142,54 +141,6 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
.getServletContext()).isNotNull();
}
@Test
public void registerBean() {
this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.register(ExampleServletWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigServletWebServerApplicationContextTests.TestBean"))
.isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithLazy() {
this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.register(ExampleServletWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigServletWebServerApplicationContextTests.TestBean"))
.isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplier() {
this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.register(ExampleServletWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, TestBean::new);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigServletWebServerApplicationContextTests.TestBean"))
.isTrue();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
@Test
public void registerBeanWithSupplierAndLazy() {
this.context = new AnnotationConfigServletWebServerApplicationContext();
this.context.register(ExampleServletWebServerApplicationConfiguration.class);
this.context.registerBean(TestBean.class, TestBean::new, Lazy.class);
this.context.refresh();
assertThat(this.context.getBeanFactory().containsSingleton(
"annotationConfigServletWebServerApplicationContextTests.TestBean"))
.isFalse();
assertThat(this.context.getBean(TestBean.class)).isNotNull();
}
private void verifyContext() {
MockServletWebServerFactory factory = this.context
.getBean(MockServletWebServerFactory.class);
......@@ -277,8 +228,4 @@ public class AnnotationConfigServletWebServerApplicationContextTests {
}
private static class TestBean {
}
}
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