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.

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

Output: @Value Property Inject name: xxxooo 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.

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

3. @ConfigurationProperties

Bind externalized configuration to a POJO and optionally validate it.

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

Output: PackProperties[name=xxxooo, age=66] Can also be used on @Bean methods.

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

4. Custom Property Files with @PropertySource

Load a custom .properties file and bind it.

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

Note: @PropertySource does not support YAML files.

5. Injecting Custom YAML Configuration

Use YamlPropertiesFactoryBean or YamlMapFactoryBean to load YAML into the environment.

pack:
  custom:
    address: localhost
    port: 8080
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()));
    }
}
@Component
@ConfigurationProperties(prefix = "pack.custom")
public class CustomProperties {
    private InetAddress address;
    private Integer port;
    @PostConstruct
    private void init() { System.out.println(this); }
    // getters & setters
}

Output:

CustomProperties[address=localhost/127.0.0.1, port=8080]

6. Accessing Properties via Environment

Retrieve any property directly from Environment.

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"));
}

Output: pack.custom.port=8080 Alternatively implement EnvironmentAware or ApplicationContextAware to obtain the environment.

7. Custom Annotation for Property Injection

Create a meta‑annotation that includes @Value.

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

8. Programmatic Loading of Properties

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

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;
}

9. Reading Command‑Line Arguments

Inject ApplicationArguments to access --key=value parameters.

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

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

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.

Spring 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

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.