Backend Development 9 min read

Mastering Spring Boot Property Injection: @Value, @ConfigurationProperties, YAML & More

This guide walks through eight Spring Boot property injection techniques—including @Value placeholders, SpEL expressions, @ConfigurationProperties, custom @PropertySource, YAML factories, Environment access, custom annotations, programmatic loading, and command‑line arguments—showing code examples and expected outputs for each method.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring Boot Property Injection: @Value, @ConfigurationProperties, YAML & More

1. @Value Dependency Injection

Inject simple properties using the ${...} placeholder syntax.

<code>pack:
  property:
    name: xxxooo
    age: 66</code>
<code>@Value("${pack.property.name}")
private String name;</code>
<code>@PostConstruct
private void init() {
    System.err.printf("@Value Property Inject name: %s%n", this.name);
}</code>

Output:

<code>@Value Property Inject name: xxxooo</code>

The @Value annotation can also be placed on parameters or methods.

2. SpEL‑Based Injection

Use Spring Expression Language with #{...} to inject bean properties or system values.

<code>@Value("#{systemProperties['java.home']}")
private String javaHome;</code>
<code>@Value("#{@systemProperties['java.home']}")
private String javaHome;</code>

3. @ConfigurationProperties

Bind externalized configuration to a POJO and optionally validate it.

<code>pack:
  property:
    name: xxxooo
    age: 66</code>
<code>@Component
@ConfigurationProperties(prefix = "pack.property")
public class PackProperties {
    private String name;
    private Integer age;
    @PostConstruct
    private void init() { System.out.println(this); }
    // getters & setters
}</code>

Output:

<code>PackProperties[name=xxxooo, age=66]</code>

Can also be used on @Bean methods.

<code>@Configuration
public class AppConfig {
    @Bean
    @ConfigurationProperties(prefix = "pack.property")
    public PackProperties packProperties() {
        return new PackProperties();
    }
}</code>

4. Custom Property Files with @PropertySource

Load a custom .properties file and bind it.

<code>pack.app.title=xxx project
pack.app.version=1.0.0</code>
<code>@Configuration
@PropertySource({"app.properties"})
public class PropertySourceConfig { }</code>
<code>@Component
@ConfigurationProperties(prefix = "pack.app")
public class AppProperties {
    private String title;
    private String version;
    // getters & setters
}</code>

Note: @PropertySource does not support YAML files.

5. Injecting Custom YAML Configuration

Use YamlPropertiesFactoryBean or YamlMapFactoryBean to load YAML into the environment.

<code>pack:
  custom:
    address: localhost
    port: 8080</code>
<code>public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ClassPathResource("pack.yml"));
        environment.getPropertySources().addLast(new PropertiesPropertySource("pack", bean.getObject()));
    }
}</code>
<code>@Component
@ConfigurationProperties(prefix = "pack.custom")
public class CustomProperties {
    private InetAddress address;
    private Integer port;
    @PostConstruct
    private void init() { System.out.println(this); }
    // getters & setters
}</code>

Output:

<code>CustomProperties[address=localhost/127.0.0.1, port=8080]</code>

6. Accessing Properties via Environment

Retrieve any property directly from Environment .

<code>private final Environment environment;
public EnvironmentGetProperties(Environment environment) { this.environment = environment; }
@PostConstruct
private void init() {
    System.err.printf("pack.custom.port=%s%n", environment.getProperty("pack.custom.port"));
}</code>

Output:

<code>pack.custom.port=8080</code>

Alternatively implement EnvironmentAware or ApplicationContextAware to obtain the environment.

7. Custom Annotation for Property Injection

Create a meta‑annotation that includes @Value .

<code>@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Value("${pack.custom.port}")
public @interface PackValue { }</code>
<code>@PackValue
private Integer port;</code>

8. Programmatic Loading of Properties

Manually read a properties file and register it as a bean.

<code>public static Properties load(String filename) {
    ClassPathResource resource = new ClassPathResource(filename);
    Properties props = new Properties();
    try { props.load(resource.getInputStream()); } catch (IOException e) { e.printStackTrace(); }
    return props;
}</code>

9. Reading Command‑Line Arguments

Inject ApplicationArguments to access --key=value parameters.

<code>private final ApplicationArguments args;
public CommandLineArgument(ApplicationArguments args) { this.args = args; }
@PostConstruct
private void init() {
    System.out.println(this.args.getOptionValues("pack.custom.port"));
}</code>

Running the application with --pack.custom.port=9999 makes the value available via the above code.

JavaSpring BootConfigurationProperties@ValueEnvironmentProperty Injection
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.