日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot中@ConfigurationProperties与@PropertySource的基本使用(读取指定的properties文件)

發布時間:2025/3/15 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot中@ConfigurationProperties与@PropertySource的基本使用(读取指定的properties文件) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

基本理論

實例


?

基本理論

@ConfigurationProperties

1. 與@Bean結合為屬性賦值;

2. 與@PropertySource(只能用于properties文件)結合讀取指定文件

@ConfigurationProperties()默認從全局配置文件中獲取值

?

因為person.properties這個不是全局文件,使用@ConfigurationProperties加載不到。如果想加載他,就得使用@PropertySource進行加載。

?

實例

程序結構如下:

從person.properties中讀取文件!

程序運行截圖如下:

源碼如下:

Dog.java

package com.sourceinport.demo.bean;public class Dog {private String name;private Integer age;@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;} }

Person.java

package com.sourceinport.demo.bean;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;import java.util.Date; import java.util.List; import java.util.Map;@PropertySource(value = {"classpath:person.properties"}) @Component @ConfigurationProperties(prefix = "person") public class Person {private String lastName;private Integer age;private Boolean boss;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;@Overridepublic String toString() {return "Person{" +"lastName='" + lastName + '\'' +", age=" + age +", boss=" + boss +", birth=" + birth +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Boolean getBoss() {return boss;}public void setBoss(Boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;} }

DemoApplication.java

package com.sourceinport.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

person.properties

person.last-name=王二麻子 person.age=18 person.birth=2019/3/4 person.boss=false person.maps.k1=v1 person.maps.k2=19 person.lists=a,b,c,d,e,f,g,h person.dog.name=小白 person.dog.age=15

DemoApplicationTests.java

package com.sourceinport.demo;import com.sourceinport.demo.bean.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@AutowiredPerson person;@Testpublic void contextLoads() {System.out.println(person);} }

?

總結

以上是生活随笔為你收集整理的Spring Boot中@ConfigurationProperties与@PropertySource的基本使用(读取指定的properties文件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。