Skip to content

Improve documentation around relaxed binding, @Value, and the canonical form of properties #20507

Closed
@asarkar

Description

@asarkar
@SpringBootApplication
@EnableConfigurationProperties(DemoProperties.class)
public class DemoApplication implements CommandLineRunner {
    @Value("${demo.itemPrice:3}")
    private int itemPrice = 2;
    @Autowired
    private DemoProperties demoProperties;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        System.out.printf("Item price from @Value: %d%n", itemPrice);
        System.out.printf("Item price from @ConfigurationProperties: %d%n", demoProperties.getItemPrice());
    }
}
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private int itemPrice = 1;

    public int getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(int itemPrice) {
        this.itemPrice = itemPrice;
    }
}

If an environment variable DEMO_ITEM_PRICE=4 is set, we get:

Item price from @Value: 3
Item price from @ConfigurationProperties: 4

If an environment variable DEMO_ITEMPRICE=4 is set, (disregarding Camel case) we get:

Item price from @Value: 4
Item price from @ConfigurationProperties: 4

Verified with 2.1.2.RELEASE. See attached demo app.
demo.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions