Merge branch '2.2.x'

Closes gh-20218
This commit is contained in:
Stephane Nicoll 2020-02-18 16:59:52 +01:00
commit 287d577aea
4 changed files with 37 additions and 6 deletions

View File

@ -198,6 +198,12 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
return this.corsConfiguration;
}
@Override
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange exchange) {
return super.getHandlerInternal(exchange)
.doOnTerminate(() -> ProducesRequestCondition.clearMediaTypesAttribute(exchange));
}
@Override
protected boolean isHandler(Class<?> beanType) {
return false;

View File

@ -231,6 +231,16 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
return this.corsConfiguration;
}
@Override
protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
try {
return super.getHandlerInternal(request);
}
finally {
ProducesRequestCondition.clearMediaTypesAttribute(request);
}
}
@Override
protected boolean isHandler(Class<?> beanType) {
return false;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -38,6 +38,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
@ -122,6 +123,10 @@ public class DataSourceAutoConfiguration {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
String url = context.getEnvironment().getProperty("spring.datasource.url");
if (StringUtils.hasText(url)) {
return ConditionOutcome.noMatch(message.found("explicit url").items(url));
}
if (anyMatches(context, metadata, this.pooledCondition)) {
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -27,6 +27,7 @@ import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Logger;
import javax.sql.DataSource;
@ -143,15 +144,19 @@ class DataSourceAutoConfigurationTests {
});
}
@Test
void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlDoesNotCreateDataSource() {
this.contextRunner.with(hideConnectionPools())
.run((context) -> assertThat(context).doesNotHaveBean(DataSource.class));
}
/**
* This test makes sure that if no supported data source is present, a datasource is
* still created if "spring.datasource.type" is present.
*/
@Test
void explicitTypeNoSupportedDataSource() {
this.contextRunner
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlAndTypeCreatesDataSource() {
this.contextRunner.with(hideConnectionPools())
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:" + SimpleDriverDataSource.class.getName())
@ -197,6 +202,11 @@ class DataSourceAutoConfigurationTests {
.isTrue());
}
private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"));
}
private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages,
Consumer<T> consumer) {
FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));