Master Spring Boot Configuration: 110 Real-World Cases & Advanced Annotation Techniques

This article introduces a free Spring Boot 3 ebook with 110 practical cases and explains the configuration file loading order, then demonstrates how to use @Value, @ConfigurationProperties, @PropertySource, EnvironmentPostProcessor and other techniques with clear code examples.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot Configuration: 110 Real-World Cases & Advanced Annotation Techniques

Spring Boot 3 practical case collection now includes 110 examples and a free PDF ebook.

1. Introduction

When Spring Boot starts, it searches configuration files in a specific order:

bootstrap.properties or bootstrap.yml (highest priority).

application.properties or application.yml (default configuration).

If spring.profiles is set, it also loads environment‑specific files such as application-dev.properties.

Note: bootstrap files are loaded only in a Spring Cloud environment.

2. Practical Cases

2.1 @Value annotation

Use @Value to inject a simple value from a configuration file.

@Service
public class TestService {
  @Value("${pack.title}")
  private String title;
  // ...
}

If the property is missing, provide a default value:

@Value("${pack.title:default_title}")
private String title;

2.2 @ConfigurationProperties annotation

For complex types, arrays or objects, use @ConfigurationProperties.

pack:
  version: 1.0.2
  title: xxxooo
@Component
@ConfigurationProperties(prefix = "pack")
public class App {
  private String title;
  private String version;
  // getters and setters
}

Map and List collections can also be bound:

@Component
@ConfigurationProperties(prefix = "pack")
public class App {
  private Map<String, Object> params = new HashMap<>();
  private List<String> addresses = new ArrayList<>();
}
pack:
  params:
    version: 1.0.0
    title: xxxooo
  addresses:
    - xxx
    - ooo

2.3 @PropertySource annotation

Load additional property files such as db.properties.

db.user=root
db.password=123456
db.url=xxxx
@Component
@PropertySource({"db.properties"})
public class DbProperties { }
@PropertySource

does not support YAML by default; a custom PropertySourceFactory is required.

2.4 EnvironmentPostProcessor

Implement EnvironmentPostProcessor for early and flexible configuration loading.

public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
  final String[] profiles = {"test.properties","bussiness.properties","pack.yml"};
  @Override
  public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    for (String profile : profiles) {
      Resource resource = new ClassPathResource(profile);
      environment.getPropertySources().addLast(loadProfiles(resource));
    }
  }
  private PropertySource<?> loadProfiles(Resource resource) { /* ... */ }
}

Register it in META-INF/spring.factories:

org.springframework.boot.env.EnvironmentPostProcessor=\
com.pack.env.PackEnvironmentPostProcessor

2.5 Other loading methods

Define a @Configuration class that creates a PropertySourcesPlaceholderConfigurer with a YamlPropertiesFactoryBean.

@Configuration
public class ConfigYaml {
  @Bean
  public static PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(new ClassPathResource("pack.yml"));
    configurer.setProperties(yaml.getObject());
    return configurer;
  }
}

Additional resources and links are provided for deeper exploration.

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.

javabackend-developmentConfigurationSpring Boot
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

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.