Understanding Spring Boot Configuration Loading Order: Apollo, @Value, and @FeignClient

This article explains why Apollo configuration stops working after switching from @Value to @FeignClient in a Spring Boot project, analyzes the loading order of Spring's configuration mechanisms, and shows how to adjust the bootstrap settings to resolve the issue.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Understanding Spring Boot Configuration Loading Order: Apollo, @Value, and @FeignClient

A developer switched a service call from using @Value to inject the URL to using @FeignClient with the URL defined in Apollo, and after deployment the application started requesting the test environment URL in production, causing runtime errors.

Original code using @Value:

@Value("${rpc.url}")
private String host;
... // build URI and execute HTTP request

After the change, the interface was defined with @FeignClient:

@FeignClient(name = "Rpc", contextId = "Rpc", url = "${rpc.url}")
public interface Rpc {
    @GetMapping(value = "xxx/xxx/query")
    Result<List<Object>> getContractDiscounts(@RequestParam("number") String number);
}

The failure was traced to Spring Boot's configuration loading sequence. Spring stores configuration in the Environment via PropertySource, and a PropertyResolver translates placeholders like ${rpc.url} at runtime.

Apollo's loading order depends on three Spring Boot phases: prepareEnvironment (earliest, loads bootstrap.yml and env vars), prepareContext (initializes context), and refreshContext (loads bean definitions). The table below summarizes the combinations of apollo.bootstrap.enabled and apollo.bootstrap.eagerLoad.enabled and the corresponding phase.

During the refreshContext phase, PropertySourcesProcessor (a BeanFactoryPostProcessor) loads Apollo properties, which means @Value injection occurs after Apollo has populated the environment, so the placeholder is resolved correctly.

In contrast, @FeignClient beans are created before BeanFactoryPostProcessor execution because the FeignClientsRegistrar (a BeanDefinitionRegistryPostProcessor) processes @FeignClient annotations during bean definition registration. Consequently, when the Feign client bean is built, the Apollo properties have not yet been loaded, and the placeholder remains unresolved, leading to the observed error.

To fix the issue, the author enabled early Apollo loading by setting apollo.bootstrap.enabled=true, which moves Apollo configuration to the prepareEnvironment phase, ensuring the placeholder is resolved before @FeignClient bean registration.

After this change, the application runs correctly in production.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ConfigurationApollospring-bootFeignClientpropertyresolver
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.