Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
45 changes: 45 additions & 0 deletions apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,49 @@
</dependency>
<!-- end of test -->
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<sourcepath>${project.build.sourceDirectory}${path.separator}${project.basedir}/src/compile-only/java</sourcepath>
<sourceFileExcludes>
<sourceFileExclude>org/springframework/boot/**</sourceFileExclude>
</sourceFileExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
Comment thread
klboke marked this conversation as resolved.
Resolved
<configuration>
<compilerArgs>
<!-- Resolve the Boot 4 SPI signature on Java 8 without emitting its class. -->
<arg>-implicit:none</arg>
<arg>-sourcepath</arg>
<arg>${project.basedir}/src/compile-only/java</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>default-cli</id>
<configuration>
<compilerArgs>
<arg>-implicit:none</arg>
<arg>-sourcepath</arg>
<arg>${project.basedir}/src/compile-only/java</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* <p>Configuration example:</p>
* <pre class="code">
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
* apollo.bootstrap.enabled = true
* </pre>
*
* or
*
* <pre class="code">
* # 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
* </pre>
*
*
* If you want to load Apollo configurations even before Logging System Initialization Phase,
* add
* <pre class="code">
* # set apollo.bootstrap.eagerLoad.enabled
* apollo.bootstrap.eagerLoad.enabled = true
* </pre>
*
* 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<ConfigurableApplicationContext>,
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<String> 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.
*
* <br />
* The processing sequence would be like this: <br />
* 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 {
}
Loading
Loading