Annotation Interface EnablePreferredConfigInjection


@Documented @Retention(RUNTIME) @Target(TYPE) @Import(ConfigAutowireCandidateResolverConfigurer.class) @API(status=STABLE, since="1.0") public @interface EnablePreferredConfigInjection
Annotation for enable injection of available Config instances based on PreferredConfig annotations.

Example:

 @Configuration
 @EnablePreferredConfigInjection
 class AppConfig {
     ...
     @Bean
     public AltConfig altConfig() {
         return Config.as(
             ...,
             AltConfig.class);
     }
     ...
     @Bean
     public MyComponent myComponent(Config config) {
         // Will be injected with default Spring environment based Config instance
         ...
     }
     ...
     @Bean
     public MyComponent myComponent(
             @PreferredConfig(AltConfig.class) Config config) {
         // Will be injected with altConfig() instance
         ...
     }
     ...
 }
 

See ConfigProviderCustomizer for details on the ConfigProvider configuration and how to customize it.

Specifying preferred configuration(s) is also supported in additional @Autowired capable injection points, allowing to use in scanned components as well.

Example:

 @Component
 class MyComponent {
     ...
     @Autowired
     public injectedByMethod(
             @PreferredConfig(AltConfig.class) Config config) {
         ...
     }
     ...
 }
 
Since:
1.0
Version:
1.0, 2025-11
Author:
(w) Iker Hernaez
See Also: