Use more flexible SpringFactoriesLoader

Closes gh-30235

Co-authored-by: Madhura Bhave <bhavem@vmware.com>
Co-authored-by: Stephane Nicoll <snicoll@vmware.com>
This commit is contained in:
Andy Wilkinson
2022-05-09 14:13:04 +01:00
parent 0fbfb8ef09
commit 770cb840c3
33 changed files with 440 additions and 311 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 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.
@@ -64,7 +64,7 @@ public class ConditionEvaluationReportLoggingListener
this(LogLevel.DEBUG);
}
public ConditionEvaluationReportLoggingListener(LogLevel logLevelForReport) {
private ConditionEvaluationReportLoggingListener(LogLevel logLevelForReport) {
Assert.isTrue(isInfoOrDebug(logLevelForReport), "LogLevel must be INFO or DEBUG");
this.logLevelForReport = logLevelForReport;
}
@@ -73,6 +73,18 @@ public class ConditionEvaluationReportLoggingListener
return LogLevel.INFO.equals(logLevelForReport) || LogLevel.DEBUG.equals(logLevelForReport);
}
/**
* Static factory method that creates a
* {@link ConditionEvaluationReportLoggingListener} which logs the report at the
* specified log level.
* @param logLevelForReport the log level to log the report at
* @return a {@link ConditionEvaluationReportLoggingListener} instance.
* @since 3.0.0
*/
public static ConditionEvaluationReportLoggingListener forLogLevel(LogLevel logLevelForReport) {
return new ConditionEvaluationReportLoggingListener(logLevelForReport);
}
public LogLevel getLogLevelForReport() {
return this.logLevelForReport;
}

View File

@@ -54,7 +54,7 @@ class BatchAutoConfigurationWithoutJpaTests {
void jdbcWithDefaultSettings() {
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true")
.withInitializer(new ConditionEvaluationReportLoggingListener(LogLevel.INFO)).run((context) -> {
.withInitializer(ConditionEvaluationReportLoggingListener.forLogLevel(LogLevel.INFO)).run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context).hasSingleBean(JobExplorer.class);
assertThat(context).hasSingleBean(JobRepository.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2022 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.
@@ -123,8 +123,8 @@ class ConditionEvaluationReportLoggingListenerTests {
@Test
void listenerWithInfoLevelShouldLogAtInfo(CapturedOutput output) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener(
LogLevel.INFO);
ConditionEvaluationReportLoggingListener initializer = ConditionEvaluationReportLoggingListener
.forLogLevel(LogLevel.INFO);
initializer.initialize(context);
context.register(Config.class);
context.refresh();
@@ -135,7 +135,7 @@ class ConditionEvaluationReportLoggingListenerTests {
@Test
void listenerSupportsOnlyInfoAndDebug() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new ConditionEvaluationReportLoggingListener(LogLevel.TRACE))
.isThrownBy(() -> ConditionEvaluationReportLoggingListener.forLogLevel(LogLevel.TRACE))
.withMessageContaining("LogLevel must be INFO or DEBUG");
}

View File

@@ -70,7 +70,7 @@ class SqlInitializationAutoConfigurationTests {
@Test
void whenConnectionFactoryIsAvailableAndModeIsNeverThenInitializerIsNotAutoConfigured() {
this.contextRunner.withConfiguration(AutoConfigurations.of(R2dbcAutoConfiguration.class))
.withInitializer(new ConditionEvaluationReportLoggingListener(LogLevel.INFO))
.withInitializer(ConditionEvaluationReportLoggingListener.forLogLevel(LogLevel.INFO))
.withPropertyValues("spring.sql.init.mode:never")
.run((context) -> assertThat(context).doesNotHaveBean(AbstractScriptDatabaseInitializer.class));
}