Spring Boot Externalized Configuration

来源:互联网 发布:2010年网络流行歌曲 编辑:程序博客网 时间:2024/06/05 20:50

Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.

使用@Value注解实现配置注入的方式某些情况下可能会非常繁琐,特别是某个配置属性为复杂数据类型(比如包含多个属性)或者配置本身为树形结构(类似Map或者List)

@Component@ConfigurationProperties(prefix="foo")public class FooProperties {    // ... see above}

实现注入

@Servicepublic class MyService {    private final FooProperties properties;    @Autowired    public MyService(FooProperties properties) {        this.properties = properties;    }     //...    @PostConstruct    public void openConnection() {        Server server = new Server(this.properties.getRemoteAddress());        // ...    }}

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

https://stackoverflow.com/questions/26699385/spring-boot-yaml-configuration-for-a-list-of-strings

阅读全文
0 0
原创粉丝点击