实践Spring Enable系列之EnableConfigurationProperties

@EnableConfigurationProperties enable support for @ConfigurationProperties annotated beans. @ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this annotation.

Configuration

1
2
3
@Configuration
// 会创建名为serverProperties的bean
@EnableConfigurationProperties(ServerProperties.class)

配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@ConfigurationProperties(prefix="my")
public class ServerProperties {
    private String name;
    private Integer port;
    private List<String> servers = new ArrayList<String>();

    public String geName(){
        return this.name;
    }

    public Integer gePort(){
        return this.port;
    }
    public List<String> getServers() {
        return this.servers;
    }
}