spring-boot 屬性定義和配置bean
阿新 • • 發佈:2018-12-06
自定義bean屬性
1.定義bean屬性
// 通過@ConfigurationProperties載入properties檔案內的配置, // 通過prefix屬性指定properties的配置的字首,通過locations指定properties檔案的位置 @ConfigurationProperties(prefix = "com.dudu") public class ConfigBean { private String name; private String want; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWant() { return want; } public void setWant(String want) { this.want = want; } } // 1.4版本的可以 通過@ConfigurationProperties載入properties檔案內的配置, // 通過prefix屬性指定properties的配置的字首,通過locations指定properties檔案的位置 //1.5版本後沒有locations屬性了,需要配合使用後@Configuration // 和@PropertySource("classpath:test.properties")來指定properties檔案的位置 @Configuration @ConfigurationProperties(prefix = "com.md") @PropertySource("classpath:test.properties") public class ConfigTestBean { private String name; private String want; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWant() { return want; } public void setWant(String want) { this.want = want; } }
2.在屬性檔案配置
3.啟用bean屬性配置
@EnableConfigurationProperties({ConfigBean.class, ConfigTestBean.class})
在配置檔案提示自定義屬性
1.加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2.mvn compile