Skip to content

Commit 77edb27

Browse files
committed
Fix package tangle caused by ApplicationContextFactory
Fix package tangle by changing `ApplicationContextFactory.DEFAULT` to use `spring.factories` to discover implementations rather than needing direct access to our own `ApplicationContext` classes. Closes gh-30272
1 parent 5c4b63b commit 77edb27

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationContextFactory.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,10 +19,9 @@
1919
import java.util.function.Supplier;
2020

2121
import org.springframework.beans.BeanUtils;
22-
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
23-
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
2422
import org.springframework.context.ConfigurableApplicationContext;
2523
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
24+
import org.springframework.core.io.support.SpringFactoriesLoader;
2625

2726
/**
2827
* Strategy interface for creating the {@link ConfigurableApplicationContext} used by a
@@ -43,14 +42,14 @@ public interface ApplicationContextFactory {
4342
*/
4443
ApplicationContextFactory DEFAULT = (webApplicationType) -> {
4544
try {
46-
switch (webApplicationType) {
47-
case SERVLET:
48-
return new AnnotationConfigServletWebServerApplicationContext();
49-
case REACTIVE:
50-
return new AnnotationConfigReactiveWebServerApplicationContext();
51-
default:
52-
return new AnnotationConfigApplicationContext();
45+
for (ApplicationContextFactory candidate : SpringFactoriesLoader
46+
.loadFactories(ApplicationContextFactory.class, ApplicationContextFactory.class.getClassLoader())) {
47+
ConfigurableApplicationContext context = candidate.create(webApplicationType);
48+
if (context != null) {
49+
return context;
50+
}
5351
}
52+
return new AnnotationConfigApplicationContext();
5453
}
5554
catch (Exception ex) {
5655
throw new IllegalStateException("Unable create a default ApplicationContext instance, "

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebServerApplicationContext.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@
2323
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2424
import org.springframework.beans.factory.support.BeanNameGenerator;
2525
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26+
import org.springframework.boot.ApplicationContextFactory;
27+
import org.springframework.boot.WebApplicationType;
28+
import org.springframework.context.ConfigurableApplicationContext;
2629
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
2730
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2831
import org.springframework.context.annotation.AnnotationConfigRegistry;
@@ -209,4 +212,18 @@ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactor
209212
}
210213
}
211214

215+
/**
216+
* {@link ApplicationContextFactory} registered in {@code spring.factories} to support
217+
* {@link AnnotationConfigReactiveWebServerApplicationContext}.
218+
*/
219+
static class Factory implements ApplicationContextFactory {
220+
221+
@Override
222+
public ConfigurableApplicationContext create(WebApplicationType webApplicationType) {
223+
return (webApplicationType != WebApplicationType.REACTIVE) ? null
224+
: new AnnotationConfigReactiveWebServerApplicationContext();
225+
}
226+
227+
}
228+
212229
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/AnnotationConfigServletWebServerApplicationContext.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@
2323
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2424
import org.springframework.beans.factory.support.BeanNameGenerator;
2525
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26+
import org.springframework.boot.ApplicationContextFactory;
27+
import org.springframework.boot.WebApplicationType;
28+
import org.springframework.context.ConfigurableApplicationContext;
2629
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
2730
import org.springframework.context.annotation.AnnotationConfigRegistry;
2831
import org.springframework.context.annotation.AnnotationConfigUtils;
@@ -206,4 +209,18 @@ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactor
206209
}
207210
}
208211

212+
/**
213+
* {@link ApplicationContextFactory} registered in {@code spring.factories} to support
214+
* {@link AnnotationConfigServletWebServerApplicationContext}.
215+
*/
216+
static class Factory implements ApplicationContextFactory {
217+
218+
@Override
219+
public ConfigurableApplicationContext create(WebApplicationType webApplicationType) {
220+
return (webApplicationType != WebApplicationType.SERVLET) ? null
221+
: new AnnotationConfigServletWebServerApplicationContext();
222+
}
223+
224+
}
225+
209226
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ org.springframework.boot.context.config.ConfigDataLoader=\
1919
org.springframework.boot.context.config.ConfigTreeConfigDataLoader,\
2020
org.springframework.boot.context.config.StandardConfigDataLoader
2121

22+
# Application Context Factories
23+
org.springframework.boot.ApplicationContextFactory=\
24+
org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext.Factory,\
25+
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.Factory
26+
2227
# Run Listeners
2328
org.springframework.boot.SpringApplicationRunListener=\
2429
org.springframework.boot.context.event.EventPublishingRunListener

0 commit comments

Comments
 (0)