diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 179bc0c9..3bca4c1a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -188,6 +188,18 @@ jobs:
java_version_prop: 17
compat_slf4j: 2.0.17
compat_vintage: 6.0.1
+ - name: spring-boot-4.1-jdk17
+ java: 17
+ spring_boot: 4.1.2
+ java_version_prop: 17
+ compat_slf4j: 2.0.20
+ compat_vintage: 6.0.3
+ - name: spring-boot-4.2-jdk17
+ java: 17
+ spring_boot: 4.2.0
+ java_version_prop: 17
+ compat_slf4j: 2.0.20
+ compat_vintage: 6.1.3
name: compat-spring-boot-${{ matrix.name }}
steps:
- uses: actions/checkout@v4
diff --git a/CHANGES.md b/CHANGES.md
index 80f4654d..7f5ba494 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -9,6 +9,7 @@ Apollo Java 2.6.0
* [Fix Apollo client local cache fallback for Spring Boot 3 executable JARs](https://github.com/apolloconfig/apollo-java/pull/136)
* [Support `extendInfo`/`parentAppId` in OpenAPI `getNamespace(s)` client calls](https://github.com/apolloconfig/apollo-java/pull/144)
* [Add an opt-in JUL to SLF4J bridge for MockWebServer logs](https://github.com/apolloconfig/apollo-java/pull/147)
+* [Fix Apollo bootstrap initialization on Spring Boot 4.2](https://github.com/apolloconfig/apollo-java/pull/152)
------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/6?closed=1)
diff --git a/apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/system/ApolloClientSystemPropertyInitializer.java b/apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/system/ApolloClientSystemPropertyInitializer.java
index 7d162bce..3a5f1339 100644
--- a/apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/system/ApolloClientSystemPropertyInitializer.java
+++ b/apollo-client-config-data/src/main/java/com/ctrip/framework/apollo/config/data/system/ApolloClientSystemPropertyInitializer.java
@@ -17,7 +17,7 @@
package com.ctrip.framework.apollo.config.data.system;
import com.ctrip.framework.apollo.config.data.util.Slf4jLogMessageFormatter;
-import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer;
+import com.ctrip.framework.apollo.spring.boot.ApolloBootstrapInitializer;
import org.apache.commons.logging.Log;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
@@ -37,7 +37,7 @@ public ApolloClientSystemPropertyInitializer(DeferredLogFactory logFactory) {
}
public void initializeSystemProperty(Binder binder, BindHandler bindHandler) {
- for (String propertyName : ApolloApplicationContextInitializer.APOLLO_SYSTEM_PROPERTIES) {
+ for (String propertyName : ApolloBootstrapInitializer.APOLLO_SYSTEM_PROPERTIES) {
this.fillSystemPropertyFromBinder(propertyName, propertyName, binder, bindHandler);
}
}
diff --git a/apollo-client/pom.xml b/apollo-client/pom.xml
index 1b51a8d9..935f3817 100644
--- a/apollo-client/pom.xml
+++ b/apollo-client/pom.xml
@@ -115,4 +115,49 @@
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+
+ ${project.build.sourceDirectory}${path.separator}${project.basedir}/src/compile-only/java
+
+ org/springframework/boot/**
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ default-compile
+
+
+
+ -implicit:none
+ -sourcepath
+ ${project.basedir}/src/compile-only/java
+
+
+
+
+ default-cli
+
+
+ -implicit:none
+ -sourcepath
+ ${project.basedir}/src/compile-only/java
+
+
+
+
+
+
+
diff --git a/apollo-client/src/compile-only/java/org/springframework/boot/EnvironmentPostProcessor.java b/apollo-client/src/compile-only/java/org/springframework/boot/EnvironmentPostProcessor.java
new file mode 100644
index 00000000..98c79826
--- /dev/null
+++ b/apollo-client/src/compile-only/java/org/springframework/boot/EnvironmentPostProcessor.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2026 Apollo 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;
+
+import org.springframework.core.env.ConfigurableEnvironment;
+
+/**
+ * Compile-only signature of the Spring Boot 4 interface for Java 8 builds.
+ */
+public interface EnvironmentPostProcessor {
+
+ void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);
+}
diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
index bfd34722..843487ca 100644
--- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
@@ -16,237 +16,13 @@
*/
package com.ctrip.framework.apollo.spring.boot;
-import com.ctrip.framework.apollo.Config;
-import com.ctrip.framework.apollo.ConfigService;
-import com.ctrip.framework.apollo.build.ApolloInjector;
-import com.ctrip.framework.apollo.core.ApolloClientSystemConsts;
-import com.ctrip.framework.apollo.core.ConfigConsts;
-import com.ctrip.framework.apollo.core.utils.DeferredLogger;
-import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource;
-import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
-import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
-import com.ctrip.framework.apollo.spring.util.PropertySourcesUtil;
-import com.ctrip.framework.apollo.spring.util.SpringInjector;
-import com.ctrip.framework.apollo.util.ConfigUtil;
-import com.google.common.base.Splitter;
-import com.google.common.base.Strings;
-import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
-import org.springframework.context.ApplicationContextInitializer;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.core.Ordered;
-import org.springframework.core.env.CompositePropertySource;
-import org.springframework.core.env.ConfigurableEnvironment;
-import org.springframework.core.env.StandardEnvironment;
/**
- * Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
- *
- *
Configuration example:
- *
- * # set app.id
- * app.id = 100004458
- * # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
- * apollo.bootstrap.enabled = true
- *
- *
- * or
- *
- *
- * # set app.id
- * app.id = 100004458
- * # enable apollo bootstrap config
- * apollo.bootstrap.enabled = true
- * # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
- * apollo.bootstrap.namespaces = application,FX.apollo
- *
- *
- *
- * If you want to load Apollo configurations even before Logging System Initialization Phase,
- * add
- *
- * # set apollo.bootstrap.eagerLoad.enabled
- * apollo.bootstrap.eagerLoad.enabled = true
- *
- *
- * This would be very helpful when your logging configurations is set by Apollo.
- *
- * for example, you have defined logback-spring.xml in your project, and you want to inject some attributes into logback-spring.xml.
+ * Apollo bootstrap initializer for callers using the legacy Spring Boot post-processor interface.
*
+ * @see ApolloBootstrapInitializer
*/
-public class ApolloApplicationContextInitializer
- implements ApplicationContextInitializer,
- EnvironmentPostProcessor, Ordered {
- public static final int DEFAULT_ORDER = 0;
-
- private static final Logger logger =
- LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
- private static final Splitter NAMESPACE_SPLITTER =
- Splitter.on(",").omitEmptyStrings().trimResults();
- public static final String[] APOLLO_SYSTEM_PROPERTIES = {ApolloClientSystemConsts.APP_ID,
- ApolloClientSystemConsts.APOLLO_LABEL, ApolloClientSystemConsts.APOLLO_CLUSTER,
- ApolloClientSystemConsts.APOLLO_CACHE_DIR, ApolloClientSystemConsts.APOLLO_ACCESS_KEY_SECRET,
- ApolloClientSystemConsts.APOLLO_META, ApolloClientSystemConsts.APOLLO_CONFIG_SERVICE,
- ApolloClientSystemConsts.APOLLO_PROPERTY_ORDER_ENABLE,
- ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE,
- ApolloClientSystemConsts.APOLLO_OVERRIDE_SYSTEM_PROPERTIES,
- ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE,
- ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_ENABLED,
- ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
- ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_JMX_ENABLED,
- ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,};
-
- private final ConfigPropertySourceFactory configPropertySourceFactory =
- SpringInjector.getInstance(ConfigPropertySourceFactory.class);
-
- private int order = DEFAULT_ORDER;
-
- @Override
- public void initialize(ConfigurableApplicationContext context) {
- ConfigurableEnvironment environment = context.getEnvironment();
-
- if (!environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class,
- false)) {
- logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}",
- context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
- return;
- }
- logger.debug("Apollo bootstrap config is enabled for context {}", context);
-
- initialize(environment);
- }
-
-
- /**
- * Initialize Apollo Configurations Just after environment is ready.
- *
- * @param environment
- */
- protected void initialize(ConfigurableEnvironment environment) {
- final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
- if (environment.getPropertySources()
- .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
- // already initialized, replay the logs that were printed before the logging system was
- // initialized
- DeferredLogger.replayTo();
- if (configUtil.isOverrideSystemProperties()) {
- // ensure ApolloBootstrapPropertySources is still the first
- PropertySourcesUtil.ensureBootstrapPropertyPrecedence(environment);
- }
- return;
- }
-
- String namespaces = environment.getProperty(
- PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
- System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, namespaces);
- logger.debug("Apollo bootstrap namespaces: {}", namespaces);
- List namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
-
- CompositePropertySource composite;
- if (configUtil.isPropertyNamesCacheEnabled()) {
- composite = new CachedCompositePropertySource(
- PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
- } else {
- composite = new CompositePropertySource(
- PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
- }
- for (String namespace : namespaceList) {
- Config config = ConfigService.getConfig(namespace);
-
- composite.addPropertySource(
- configPropertySourceFactory.getConfigPropertySource(namespace, config));
- }
- if (!configUtil.isOverrideSystemProperties()) {
- if (environment.getPropertySources()
- .contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
- environment.getPropertySources()
- .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
- return;
- }
- }
- environment.getPropertySources().addFirst(composite);
- }
-
- /**
- * To fill system properties from environment config
- */
- void initializeSystemProperty(ConfigurableEnvironment environment) {
- for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
- fillSystemPropertyFromEnvironment(environment, propertyName);
- }
- }
-
- private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment,
- String propertyName) {
- if (System.getProperty(propertyName) != null) {
- return;
- }
-
- String propertyValue = environment.getProperty(propertyName);
-
- if (Strings.isNullOrEmpty(propertyValue)) {
- return;
- }
-
- System.setProperty(propertyName, propertyValue);
- }
-
- /**
- *
- * In order to load Apollo configurations as early as even before Spring loading logging system phase,
- * this EnvironmentPostProcessor can be called Just After ConfigFileApplicationListener has succeeded.
- *
- *
- * The processing sequence would be like this:
- * Load Bootstrap properties and application properties -----> load Apollo configuration properties ----> Initialize Logging systems
- *
- * @param configurableEnvironment
- * @param springApplication
- */
- @Override
- public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment,
- SpringApplication springApplication) {
-
- // should always initialize system properties like app.id in the first place
- initializeSystemProperty(configurableEnvironment);
-
- Boolean eagerLoadEnabled = configurableEnvironment.getProperty(
- PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.class, false);
- System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
- String.valueOf(eagerLoadEnabled));
- // EnvironmentPostProcessor should not be triggered if you don't want Apollo Loading before
- // Logging System Initialization
- if (!eagerLoadEnabled) {
- return;
- }
-
- Boolean bootstrapEnabled = configurableEnvironment
- .getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false);
- System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED,
- String.valueOf(bootstrapEnabled));
- if (bootstrapEnabled) {
- DeferredLogger.enable();
- initialize(configurableEnvironment);
- }
-
- }
-
- /**
- * @since 1.3.0
- */
- @Override
- public int getOrder() {
- return order;
- }
-
- /**
- * @since 1.3.0
- */
- public void setOrder(int order) {
- this.order = order;
- }
-
+public class ApolloApplicationContextInitializer extends ApolloBootstrapInitializer
+ implements EnvironmentPostProcessor {
}
diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBoot4EnvironmentPostProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBoot4EnvironmentPostProcessor.java
new file mode 100644
index 00000000..201361a9
--- /dev/null
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBoot4EnvironmentPostProcessor.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2026 Apollo 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 com.ctrip.framework.apollo.spring.boot;
+
+import org.springframework.boot.EnvironmentPostProcessor;
+import org.springframework.boot.SpringApplication;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.util.ClassUtils;
+
+/**
+ * Runs Apollo bootstrap in the Spring Boot 4 environment post-processor chain.
+ */
+public class ApolloBoot4EnvironmentPostProcessor extends ApolloBootstrapInitializer
+ implements EnvironmentPostProcessor {
+
+ /**
+ * Entry point for automatic registration when Boot may also discover the legacy SPI.
+ */
+ public static class FactoryRegistration extends ApolloBoot4EnvironmentPostProcessor {
+
+ private static final boolean LEGACY_PROCESSOR_PRESENT =
+ ClassUtils.isPresent("org.springframework.boot.env.EnvironmentPostProcessor",
+ FactoryRegistration.class.getClassLoader());
+
+ @Override
+ public void postProcessEnvironment(ConfigurableEnvironment environment,
+ SpringApplication application) {
+ // Boot 4.0 and 4.1 invoke Apollo through the legacy registration in the same chain.
+ if (!LEGACY_PROCESSOR_PRESENT) {
+ super.postProcessEnvironment(environment, application);
+ }
+ }
+ }
+}
diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBootstrapInitializer.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBootstrapInitializer.java
new file mode 100644
index 00000000..111b9b84
--- /dev/null
+++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloBootstrapInitializer.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2022 Apollo 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 com.ctrip.framework.apollo.spring.boot;
+
+import com.ctrip.framework.apollo.Config;
+import com.ctrip.framework.apollo.ConfigService;
+import com.ctrip.framework.apollo.build.ApolloInjector;
+import com.ctrip.framework.apollo.core.ApolloClientSystemConsts;
+import com.ctrip.framework.apollo.core.ConfigConsts;
+import com.ctrip.framework.apollo.core.utils.DeferredLogger;
+import com.ctrip.framework.apollo.spring.config.CachedCompositePropertySource;
+import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
+import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
+import com.ctrip.framework.apollo.spring.util.PropertySourcesUtil;
+import com.ctrip.framework.apollo.spring.util.SpringInjector;
+import com.ctrip.framework.apollo.util.ConfigUtil;
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.SpringApplication;
+import org.springframework.context.ApplicationContextInitializer;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.core.Ordered;
+import org.springframework.core.env.CompositePropertySource;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.StandardEnvironment;
+
+/**
+ * Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
+ *
+ * Configuration example:
+ *
+ * # set app.id
+ * app.id = 100004458
+ * # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
+ * apollo.bootstrap.enabled = true
+ *
+ *
+ * or
+ *
+ *
+ * # set app.id
+ * app.id = 100004458
+ * # enable apollo bootstrap config
+ * apollo.bootstrap.enabled = true
+ * # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
+ * apollo.bootstrap.namespaces = application,FX.apollo
+ *
+ *
+ *
+ * If you want to load Apollo configurations even before Logging System Initialization Phase,
+ * add
+ *
+ * # set apollo.bootstrap.eagerLoad.enabled
+ * apollo.bootstrap.eagerLoad.enabled = true
+ *
+ *
+ * This would be very helpful when your logging configurations is set by Apollo.
+ *
+ * for example, you have defined logback-spring.xml in your project, and you want to inject some attributes into logback-spring.xml.
+ *
+ */
+public class ApolloBootstrapInitializer
+ implements ApplicationContextInitializer, Ordered {
+ public static final int DEFAULT_ORDER = 0;
+
+ // Use the original logging category without loading the legacy SPI class on Boot 4.2.
+ private static final Logger logger = LoggerFactory
+ .getLogger("com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer");
+ private static final Splitter NAMESPACE_SPLITTER =
+ Splitter.on(",").omitEmptyStrings().trimResults();
+ public static final String[] APOLLO_SYSTEM_PROPERTIES = {ApolloClientSystemConsts.APP_ID,
+ ApolloClientSystemConsts.APOLLO_LABEL, ApolloClientSystemConsts.APOLLO_CLUSTER,
+ ApolloClientSystemConsts.APOLLO_CACHE_DIR, ApolloClientSystemConsts.APOLLO_ACCESS_KEY_SECRET,
+ ApolloClientSystemConsts.APOLLO_META, ApolloClientSystemConsts.APOLLO_CONFIG_SERVICE,
+ ApolloClientSystemConsts.APOLLO_PROPERTY_ORDER_ENABLE,
+ ApolloClientSystemConsts.APOLLO_PROPERTY_NAMES_CACHE_ENABLE,
+ ApolloClientSystemConsts.APOLLO_OVERRIDE_SYSTEM_PROPERTIES,
+ ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_TYPE,
+ ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_ENABLED,
+ ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXTERNAL_EXPORT_PERIOD,
+ ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_JMX_ENABLED,
+ ApolloClientSystemConsts.APOLLO_CLIENT_MONITOR_EXCEPTION_QUEUE_SIZE,};
+
+ private final ConfigPropertySourceFactory configPropertySourceFactory =
+ SpringInjector.getInstance(ConfigPropertySourceFactory.class);
+
+ private int order = DEFAULT_ORDER;
+
+ @Override
+ public void initialize(ConfigurableApplicationContext context) {
+ ConfigurableEnvironment environment = context.getEnvironment();
+
+ if (!environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class,
+ false)) {
+ logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}",
+ context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
+ return;
+ }
+ logger.debug("Apollo bootstrap config is enabled for context {}", context);
+
+ initialize(environment);
+ }
+
+
+ /**
+ * Initialize Apollo Configurations Just after environment is ready.
+ *
+ * @param environment
+ */
+ protected void initialize(ConfigurableEnvironment environment) {
+ final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
+ if (environment.getPropertySources()
+ .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
+ // already initialized, replay the logs that were printed before the logging system was
+ // initialized
+ DeferredLogger.replayTo();
+ if (configUtil.isOverrideSystemProperties()) {
+ // ensure ApolloBootstrapPropertySources is still the first
+ PropertySourcesUtil.ensureBootstrapPropertyPrecedence(environment);
+ }
+ return;
+ }
+
+ String namespaces = environment.getProperty(
+ PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, namespaces);
+ logger.debug("Apollo bootstrap namespaces: {}", namespaces);
+ List namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
+
+ CompositePropertySource composite;
+ if (configUtil.isPropertyNamesCacheEnabled()) {
+ composite = new CachedCompositePropertySource(
+ PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
+ } else {
+ composite = new CompositePropertySource(
+ PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
+ }
+ for (String namespace : namespaceList) {
+ Config config = ConfigService.getConfig(namespace);
+
+ composite.addPropertySource(
+ configPropertySourceFactory.getConfigPropertySource(namespace, config));
+ }
+ if (!configUtil.isOverrideSystemProperties()) {
+ if (environment.getPropertySources()
+ .contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
+ environment.getPropertySources()
+ .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, composite);
+ return;
+ }
+ }
+ environment.getPropertySources().addFirst(composite);
+ }
+
+ /**
+ * To fill system properties from environment config
+ */
+ void initializeSystemProperty(ConfigurableEnvironment environment) {
+ for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
+ fillSystemPropertyFromEnvironment(environment, propertyName);
+ }
+ }
+
+ private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment,
+ String propertyName) {
+ if (System.getProperty(propertyName) != null) {
+ return;
+ }
+
+ String propertyValue = environment.getProperty(propertyName);
+
+ if (Strings.isNullOrEmpty(propertyValue)) {
+ return;
+ }
+
+ System.setProperty(propertyName, propertyValue);
+ }
+
+ /**
+ * Prepares Apollo configuration after Spring Boot loads the application environment and before
+ * it initializes logging.
+ *
+ * @param configurableEnvironment the prepared application environment
+ * @param springApplication the Spring Boot application
+ */
+ public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment,
+ SpringApplication springApplication) {
+
+ // should always initialize system properties like app.id in the first place
+ initializeSystemProperty(configurableEnvironment);
+
+ Boolean eagerLoadEnabled = configurableEnvironment.getProperty(
+ PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, Boolean.class, false);
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED,
+ String.valueOf(eagerLoadEnabled));
+ // EnvironmentPostProcessor should not be triggered if you don't want Apollo Loading before
+ // Logging System Initialization
+ if (!eagerLoadEnabled) {
+ return;
+ }
+
+ Boolean bootstrapEnabled = configurableEnvironment
+ .getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, Boolean.class, false);
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED,
+ String.valueOf(bootstrapEnabled));
+ if (bootstrapEnabled) {
+ DeferredLogger.enable();
+ initialize(configurableEnvironment);
+ }
+
+ }
+
+ /**
+ * @since 1.3.0
+ */
+ @Override
+ public int getOrder() {
+ return order;
+ }
+
+ /**
+ * @since 1.3.0
+ */
+ public void setOrder(int order) {
+ this.order = order;
+ }
+
+}
diff --git a/apollo-client/src/main/resources/META-INF/spring.factories b/apollo-client/src/main/resources/META-INF/spring.factories
index 80c3b509..ba9dcc71 100644
--- a/apollo-client/src/main/resources/META-INF/spring.factories
+++ b/apollo-client/src/main/resources/META-INF/spring.factories
@@ -1,6 +1,8 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration
org.springframework.context.ApplicationContextInitializer=\
-com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer
+com.ctrip.framework.apollo.spring.boot.ApolloBootstrapInitializer
org.springframework.boot.env.EnvironmentPostProcessor=\
com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer
+org.springframework.boot.EnvironmentPostProcessor=\
+com.ctrip.framework.apollo.spring.boot.ApolloBoot4EnvironmentPostProcessor$FactoryRegistration
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java
index daf9cd4b..72c3c0e6 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/BootstrapConfigTest.java
@@ -19,8 +19,10 @@
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
+import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer;
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
import com.google.common.collect.Sets;
+import java.util.Collections;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -30,18 +32,21 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.core.io.support.SpringFactoriesLoader;
+import org.springframework.core.Ordered;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import java.util.List;
-
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -401,20 +406,25 @@ public void test() throws Exception {
}
}
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringBootTest(classes = ConfigurationWithoutConditionalOnProperty.class)
- @DirtiesContext
public static class TestWithBootstrapEnabledAndEagerLoadEnabled
extends AbstractSpringIntegrationTest {
+ private static final String EAGER_PROPERTY = "apollo.test.eager-property";
+ private static final String PROBE_ENABLED = "apollo.test.bootstrap-probe.enabled";
+ private static final String PROBE_VALUE = "apollo.test.bootstrap-probe.value";
+
@BeforeClass
public static void beforeClass() throws Exception {
doSetUp();
System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "true");
System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, "true");
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES,
+ ConfigConsts.NAMESPACE_APPLICATION);
Config config = mock(Config.class);
+ when(config.getPropertyNames()).thenReturn(Collections.singleton(EAGER_PROPERTY));
+ when(config.getProperty(EAGER_PROPERTY, null)).thenReturn("from-apollo");
mockConfig(someAppId, ConfigConsts.NAMESPACE_APPLICATION, config);
}
@@ -423,23 +433,39 @@ public static void beforeClass() throws Exception {
public static void afterClass() {
System.clearProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
System.clearProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED);
+ System.clearProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES);
doTearDown();
}
@Test
- public void test() {
- List names = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class,
- getClass().getClassLoader());
- boolean containsApollo = false;
- for (String name : names) {
- if (name
- .equals("com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer")) {
- containsApollo = true;
- break;
+ public void testLoadsBootstrapPropertiesBeforeLaterEnvironmentPostProcessors() {
+ SpringApplication application =
+ new SpringApplication(ConfigurationWithoutConditionalOnProperty.class);
+ application.setWebApplicationType(WebApplicationType.NONE);
+ application.setDefaultProperties(Collections.singletonMap(PROBE_ENABLED, true));
+
+ try (ConfigurableApplicationContext context = application.run()) {
+ Assert.assertEquals("from-apollo", context.getEnvironment().getProperty(PROBE_VALUE));
+ }
+ }
+
+ public static class BootstrapProbe implements EnvironmentPostProcessor, Ordered {
+
+ @Override
+ public int getOrder() {
+ return ApolloApplicationContextInitializer.DEFAULT_ORDER + 1;
+ }
+
+ @Override
+ public void postProcessEnvironment(ConfigurableEnvironment environment,
+ SpringApplication application) {
+ if (environment.getProperty(PROBE_ENABLED, Boolean.class, false)) {
+ String observedValue = environment.getProperty(EAGER_PROPERTY, "missing");
+ environment.getPropertySources().addFirst(new MapPropertySource("apolloBootstrapProbe",
+ Collections.singletonMap(PROBE_VALUE, observedValue)));
}
}
- Assert.assertTrue(containsApollo);
}
}
diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
index c428a3fb..e75c07b3 100644
--- a/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
+++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
@@ -30,6 +30,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
@@ -56,6 +57,11 @@ public void tearDown() throws Exception {
MockInjector.reset();
}
+ @Test
+ public void remainsAssignableToTheLegacyEnvironmentPostProcessor() {
+ assertTrue(EnvironmentPostProcessor.class.isInstance(apolloApplicationContextInitializer));
+ }
+
@Test
public void testFillFromEnvironment() throws Exception {
String someAppId = "someAppId";
diff --git a/apollo-client/src/test/resources/META-INF/spring.factories b/apollo-client/src/test/resources/META-INF/spring.factories
new file mode 100644
index 00000000..9c4c63e6
--- /dev/null
+++ b/apollo-client/src/test/resources/META-INF/spring.factories
@@ -0,0 +1,2 @@
+org.springframework.boot.env.EnvironmentPostProcessor=\
+com.ctrip.framework.apollo.spring.BootstrapConfigTest$TestWithBootstrapEnabledAndEagerLoadEnabled$BootstrapProbe
diff --git a/apollo-compat-tests/apollo-spring-boot-compat-it/pom.xml b/apollo-compat-tests/apollo-spring-boot-compat-it/pom.xml
index 5a8efa46..0c3597ca 100644
--- a/apollo-compat-tests/apollo-spring-boot-compat-it/pom.xml
+++ b/apollo-compat-tests/apollo-spring-boot-compat-it/pom.xml
@@ -91,9 +91,29 @@
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ default-testCompile
+
+
+ -implicit:none
+ -sourcepath
+ ${project.basedir}/../../apollo-client/src/compile-only/java
+
+
+
+
+
org.apache.maven.plugins
maven-surefire-plugin
+
+
+ false
+
diff --git a/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/java/com/ctrip/framework/apollo/compat/springboot/ApolloBoot4BootstrapCompatibilityTest.java b/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/java/com/ctrip/framework/apollo/compat/springboot/ApolloBoot4BootstrapCompatibilityTest.java
new file mode 100644
index 00000000..e1f0571a
--- /dev/null
+++ b/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/java/com/ctrip/framework/apollo/compat/springboot/ApolloBoot4BootstrapCompatibilityTest.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2026 Apollo 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 com.ctrip.framework.apollo.compat.springboot;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+import com.ctrip.framework.apollo.mockserver.ApolloTestingServer;
+import com.ctrip.framework.apollo.spring.boot.ApolloBoot4EnvironmentPostProcessor;
+import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import org.junit.Test;
+import org.springframework.boot.EnvironmentPostProcessor;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.WebApplicationType;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.Ordered;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MapPropertySource;
+import org.springframework.core.env.StandardEnvironment;
+import org.springframework.util.ClassUtils;
+
+public class ApolloBoot4BootstrapCompatibilityTest {
+
+ private static final String ENABLE_PROBE = "apollo.compat.bootstrap-probe.enabled";
+ private static final String APOLLO_VALUE = "apollo.compat.bootstrap-only-value";
+ private static final String OBSERVED_VALUE = "apollo.compat.bootstrap-probe.value";
+ private static final String MODERN_PROCESSOR =
+ "org.springframework.boot.EnvironmentPostProcessor";
+
+ @Test
+ public void laterProcessorSeesApolloValueAndBootstrapRunsOnce() throws Exception {
+ ClassLoader classLoader = getClass().getClassLoader();
+ assumeTrue(ClassUtils.isPresent(MODERN_PROCESSOR, classLoader));
+ String processorResource = MODERN_PROCESSOR.replace('.', '/') + ".class";
+ assertEquals(1, Collections.list(classLoader.getResources(processorResource)).size());
+
+ Properties originalProperties = (Properties) System.getProperties().clone();
+ System.setProperty("app.id", "someAppId");
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, "application");
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "true");
+ System.setProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, "true");
+ try (ApolloTestingServer server = new ApolloTestingServer()) {
+ server.start();
+ server.addOrModifyProperty("application", APOLLO_VALUE, "from-apollo");
+ CountingEnvironment environment = new CountingEnvironment();
+ assertBootstrap(application(environment), environment);
+
+ CountingEnvironment explicitEnvironment = new CountingEnvironment();
+ SpringApplication explicitApplication = application(explicitEnvironment);
+ ApplicationListener> explicitListener = explicitListener(classLoader);
+ List> listeners = new ArrayList<>(explicitApplication.getListeners());
+ listeners.removeIf(listener -> listener.getClass() == explicitListener.getClass());
+ listeners.add(explicitListener);
+ explicitApplication.setListeners(listeners);
+ assertBootstrap(explicitApplication, explicitEnvironment);
+ } finally {
+ System.setProperties(originalProperties);
+ }
+ }
+
+ private SpringApplication application(CountingEnvironment environment) {
+ SpringApplication application = new SpringApplication(TestConfiguration.class);
+ application.setWebApplicationType(WebApplicationType.NONE);
+ application.setDefaultProperties(Collections.singletonMap(ENABLE_PROBE, true));
+ application.setEnvironment(environment);
+ return application;
+ }
+
+ private void assertBootstrap(SpringApplication application, CountingEnvironment environment) {
+ try (ConfigurableApplicationContext context = application.run()) {
+ assertEquals("from-apollo", context.getEnvironment().getProperty(OBSERVED_VALUE));
+ assertEquals(1, environment.bootstrapReads);
+ }
+ }
+
+ private ApplicationListener> explicitListener(ClassLoader classLoader)
+ throws ReflectiveOperationException {
+ // The factory API is only available on Boot 4, but this suite also compiles on Boot 2 and 3.
+ Class> factory =
+ classLoader.loadClass("org.springframework.boot.support.EnvironmentPostProcessorsFactory");
+ Class> listener = classLoader
+ .loadClass("org.springframework.boot.support.EnvironmentPostProcessorApplicationListener");
+ Object processors = factory.getMethod("of", Class[].class).invoke(null,
+ (Object) new Class>[] {ApolloBoot4EnvironmentPostProcessor.class, LaterProcessor.class});
+ return (ApplicationListener>) listener.getMethod("with", factory).invoke(null, processors);
+ }
+
+ @Configuration
+ static class TestConfiguration {
+ }
+
+ private static class CountingEnvironment extends StandardEnvironment {
+
+ private int bootstrapReads;
+
+ @Override
+ public T getProperty(String key, Class targetType, T defaultValue) {
+ if (PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED.equals(key)) {
+ bootstrapReads++;
+ }
+ return super.getProperty(key, targetType, defaultValue);
+ }
+ }
+
+ public static class LaterProcessor implements EnvironmentPostProcessor, Ordered {
+
+ @Override
+ public int getOrder() {
+ return 1;
+ }
+
+ @Override
+ public void postProcessEnvironment(ConfigurableEnvironment environment,
+ SpringApplication application) {
+ if (environment.getProperty(ENABLE_PROBE, Boolean.class, false)) {
+ String value = environment.getProperty(APOLLO_VALUE, "missing");
+ environment.getPropertySources().addFirst(new MapPropertySource("apolloBootstrapProbe",
+ Collections.singletonMap(OBSERVED_VALUE, value)));
+ }
+ }
+ }
+}
diff --git a/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/resources/META-INF/spring.factories b/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/resources/META-INF/spring.factories
new file mode 100644
index 00000000..74fc1472
--- /dev/null
+++ b/apollo-compat-tests/apollo-spring-boot-compat-it/src/test/resources/META-INF/spring.factories
@@ -0,0 +1,2 @@
+org.springframework.boot.EnvironmentPostProcessor=\
+com.ctrip.framework.apollo.compat.springboot.ApolloBoot4BootstrapCompatibilityTest$LaterProcessor