Backend Development 8 min read

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.

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

If the property is missing, provide a default value:

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

2.2 @ConfigurationProperties annotation

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

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

Map and List collections can also be bound:

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

2.3 @PropertySource annotation

Load additional property files such as db.properties .

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

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

2.4 EnvironmentPostProcessor

Implement EnvironmentPostProcessor for early and flexible configuration loading.

<code>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) { /* ... */ }
}
</code>

Register it in META-INF/spring.factories :

<code>org.springframework.boot.env.EnvironmentPostProcessor=\
com.pack.env.PackEnvironmentPostProcessor
</code>

2.5 Other loading methods

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

<code>@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;
  }
}
</code>

Additional resources and links are provided for deeper exploration.

Javabackend developmentconfigurationSpring BootAnnotations
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

login 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.