Is it possible to get custom object from spring application properties?

后端 未结 1 1200
盖世英雄少女心
盖世英雄少女心 2021-01-20 01:35

Is it possible to get own object from application.yaml and bind it with @Value to my component?

Model:

@Data
public class CurrencyPlan {
    private          


        
相关标签:
1条回答
  • 2021-01-20 02:10

    If you're looking to bind your properties to a class you can use @ConfigurationProperties.

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

    @ConfigurationProperties(prefix="plans.eur") and @Component would be placed on the CurrencyPlan. @EnableConfigurationProperties preferably placed on an @Configuration class.

    After you can autowire the CurrencyPlan class into the dependent classes.

    0 讨论(0)
提交回复
热议问题