SpringBoot中@ConfigurationProperties注解實現配置綁定的三種方法
properties配置文件如下:
human.name=Mr.Yu human.age=21 human.gender=male
如何把properties里面的配置綁定到JavaBean里面,以前我們的做法如下:
public class PropertiesUtil { public static void getProperties(Person person) throws IOException { Properties properties = new Properties(); properties.load(new FileInputStream("src/main/resources/demo.properties")); //得到配置文件key的名字 Enumeration enumeration = properties.propertyNames(); while (enumeration.hasMoreElements()){ String key =(String)enumeration.nextElement(); String value = properties.getProperty(key); System.out.println("key="+key+"——————value="+value); //封裝到JavaBean //以下內容省略 } } }
輸出結果:
key=human.name——————value=Mr.Yu
key=human.age——————value=21
key=human.gender——————value=maleProcess finished with exit code 0
可以看到這個過程十分繁雜,但是在SpringBoot中這個過程將會變得非常簡單。
在SpringBoot中有如下3種方法:
1.@ConfigurationProperties注解+@Component(或@Controller或@Service或@Repository)注解
- 只有在容器中的組件,才會擁有SpringBoot提供的強大功能,也就是如果我們需要使用到@ConfigurationProperties注解,那么我們首先要保證該對JavaBean對象在IoC容器中,所以需要用到Component注解來添加組件到容器中。
- @ConfigurationProperties注解中prefix與value互相都是別名
- 也就是說
@ConfigurationProperties(value = "human")
與@ConfigurationProperties(prefix = "human")
是一樣的 - prefix和value指的是前綴的意思
代碼測試: properties配置文件:
human.name=Mr.Yu human.age=21 human.gender=male
Person類:
@Component ////只有在容器中的組件,才會擁有SpringBoot提供的強大功能 @ConfigurationProperties(value = "human") public class Person { public String name; public int age; public String gender; public Person() { } public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
測試類如下:
//@Controller ////@ResponseBody以字符串的方式寫給瀏覽器,而不是跳轉到某個頁面 //@ResponseBody //@RestController = @Controller + @ResponseBody @RestController public class HelloController { //自動注入屬性 @Autowired Person person; @RequestMapping("/person") public Person getPerson(){ return person; } }
測試結果:
@ConfigurationProperties注解+@EnableConfigurationProperties注解
這種方式@EnableConfigurationProperties注解一定要在配置類上寫,@EnableConfigurationProperties的意思是開啟屬性配置功能,開啟誰的屬性配置功能呢,因為我們上面的Person類想進行配置綁定,所以我們在后面加上參數Person.class:@EnableConfigurationProperties(Person.class)
@EnableConfigurationProperties(Person.class)的作用就是把這個Person組件注冊到容器中,對象的名稱為:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前綴value的值
在Person類上還是有@ConfigurationProperties注解,這種方式把Person類上的@Component注解換成了配置類上的@EnableConfigurationProperties(Person.class)注解
這種方式主要用在引用第三方包時,比如第三方包中有一個Person類,該類中沒有使用@Component,我們也不能夠給第三方包中的類加上@Component,這個時候就可以使用在配置類上加上@EnableConfigurationProperties注解的方法。
@ConfigurationProperties注解+@Import注解
- 使用@Import給容器中自動創(chuàng)建出這個類型的組件、默認組件的名字就是全類名
- @Import注解與2中的@ConfigurationProperties注解效果是一樣的
- 只不過他們注冊到容器中的名稱不同:
- @ConfigurationProperties注解注冊到容器中對象的名稱為:human-com.ysw.boot.properties.Person 其中human是@ConfigurationProperties(value = "human")中前綴value的值
- @Import注解注冊到容器中對象的名稱為:com.ysw.boot.properties.Person
配置類:
Person類:
測試類:
application.properties文件:
server.port=8888 human.name=Mr.Yu human.age=21 human.gender=male222
測試結果:
到此這篇關于SpringBoot中@ConfigurationProperties注解實現配置綁定的三種方法的文章就介紹到這了,更多相關SpringBoot配置綁定方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot中的@ConfigurationProperties注解解析
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- 關于SpringBoot的@ConfigurationProperties注解和松散綁定、數據校驗
- SpringBoot2底層注解@ConfigurationProperties配置綁定
- SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用詳解
- SpringBoot @ConfigurationProperties注解的簡單使用
- Springboot之@ConfigurationProperties注解解讀
相關文章
Spring Boot中l(wèi)ombok的安裝與使用詳解
這篇文章主要給大家介紹了關于Spring Boot中l(wèi)ombok安裝與使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-09-09SpringBoot中@Autowired注入service時出現循環(huán)依賴問題的解決方法
在Spring Boot開發(fā)過程中,@Autowired注入Service時出現循環(huán)依賴是一個常見問題,循環(huán)依賴指的是兩個或多個Bean相互依賴,形成閉環(huán),導致Spring容器無法正常初始化這些Bean,這里提供幾種解決Spring Boot中@Autowired注入Service時循環(huán)依賴問題的方法2024-02-02