Commit de50cfa2 authored by Phillip Webb's avatar Phillip Webb

Make OnClassCondition an AutoConfigurationImportFilter

Update OnClassCondition to implement AutoConfigurationImportFilter so
that auto-configuration candidates can be filtered early. The
optimization helps to improve application startup time by reducing
the number of classes that are loaded.

See gh-7573
parent 20a20b77
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2017 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.
...@@ -87,7 +87,7 @@ public abstract class SpringBootCondition implements Condition { ...@@ -87,7 +87,7 @@ public abstract class SpringBootCondition implements Condition {
+ methodMetadata.getMethodName(); + methodMetadata.getMethodName();
} }
private void logOutcome(String classOrMethodName, ConditionOutcome outcome) { protected final void logOutcome(String classOrMethodName, ConditionOutcome outcome) {
if (this.logger.isTraceEnabled()) { if (this.logger.isTraceEnabled()) {
this.logger.trace(getLogMessage(classOrMethodName, outcome)); this.logger.trace(getLogMessage(classOrMethodName, outcome));
} }
......
...@@ -11,6 +11,10 @@ org.springframework.boot.autoconfigure.BackgroundPreinitializer ...@@ -11,6 +11,10 @@ org.springframework.boot.autoconfigure.BackgroundPreinitializer
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnClassCondition
# Auto Configure # Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
......
/*
* Copyright 2012-2017 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
*
* http://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.autoconfigure.condition;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import org.springframework.core.io.support.SpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for the {@link AutoConfigurationImportFilter} part of {@link OnClassCondition}.
*
* @author Phillip Webb
*/
public class OnClassConditionAutoConfigurationImportFilterTests {
private OnClassCondition filter = new OnClassCondition();
private DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@Before
public void setup() {
this.filter.setBeanClassLoader(getClass().getClassLoader());
this.filter.setBeanFactory(this.beanFactory);
}
@Test
public void shouldBeRegistered() throws Exception {
assertThat(SpringFactoriesLoader
.loadFactories(AutoConfigurationImportFilter.class, null))
.hasAtLeastOneElementOfType(OnClassCondition.class);
}
@Test
public void matchShouldMatchClasses() throws Exception {
String[] autoConfigurationClasses = new String[] { "test.match", "test.nomatch" };
boolean[] result = this.filter.match(autoConfigurationClasses,
getAutoConfigurationMetadata());
assertThat(result).containsExactly(true, false);
}
@Test
public void matchShouldRecordOutcome() throws Exception {
String[] autoConfigurationClasses = new String[] { "test.match", "test.nomatch" };
this.filter.match(autoConfigurationClasses, getAutoConfigurationMetadata());
ConditionEvaluationReport report = ConditionEvaluationReport
.get(this.beanFactory);
assertThat(report.getConditionAndOutcomesBySource()).hasSize(1)
.containsKey("test.nomatch");
}
private AutoConfigurationMetadata getAutoConfigurationMetadata() {
AutoConfigurationMetadata metadata = mock(AutoConfigurationMetadata.class);
given(metadata.wasProcessed("test.match")).willReturn(true);
given(metadata.getSet("test.match", "ConditionalOnClass"))
.willReturn(Collections.<String>singleton("java.io.InputStream"));
given(metadata.wasProcessed("test.nomatch")).willReturn(true);
given(metadata.getSet("test.nomatch", "ConditionalOnClass"))
.willReturn(Collections.<String>singleton("java.io.DoesNotExist"));
return metadata;
}
}
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