Browse Source

Stop resolving AsyncConfigurer instances eagerly

Closes gh-27808
pull/27832/head
Stephane Nicoll 4 years ago
parent
commit
de10bb69cb
  1. 40
      spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java

40
spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 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.
@ -16,11 +16,14 @@
package org.springframework.scheduling.annotation; package org.springframework.scheduling.annotation;
import java.util.Collection; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware; import org.springframework.context.annotation.ImportAware;
@ -28,6 +31,7 @@ import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.function.SingletonSupplier;
/** /**
* Abstract base {@code Configuration} class providing common structure for enabling * Abstract base {@code Configuration} class providing common structure for enabling
@ -65,17 +69,27 @@ public abstract class AbstractAsyncConfiguration implements ImportAware {
/** /**
* Collect any {@link AsyncConfigurer} beans through autowiring. * Collect any {@link AsyncConfigurer} beans through autowiring.
*/ */
@Autowired(required = false) @Autowired
void setConfigurers(Collection<AsyncConfigurer> configurers) { void setConfigurers(ObjectProvider<AsyncConfigurer> configurers) {
if (CollectionUtils.isEmpty(configurers)) { Supplier<AsyncConfigurer> asyncConfigurer = SingletonSupplier.of(() -> {
return; List<AsyncConfigurer> candidates = configurers.stream().collect(Collectors.toList());
} if (CollectionUtils.isEmpty(candidates)) {
if (configurers.size() > 1) { return null;
throw new IllegalStateException("Only one AsyncConfigurer may exist"); }
} if (candidates.size() > 1) {
AsyncConfigurer configurer = configurers.iterator().next(); throw new IllegalStateException("Only one AsyncConfigurer may exist");
this.executor = configurer::getAsyncExecutor; }
this.exceptionHandler = configurer::getAsyncUncaughtExceptionHandler; return candidates.get(0);
});
this.executor = adapt(asyncConfigurer, AsyncConfigurer::getAsyncExecutor);
this.exceptionHandler = adapt(asyncConfigurer, AsyncConfigurer::getAsyncUncaughtExceptionHandler);
}
private <T> Supplier<T> adapt(Supplier<AsyncConfigurer> supplier, Function<AsyncConfigurer, T> provider) {
return () -> {
AsyncConfigurer asyncConfigurer = supplier.get();
return (asyncConfigurer != null) ? provider.apply(asyncConfigurer) : null;
};
} }
} }

Loading…
Cancel
Save