javascript
Spring Boot读取peoperties配置及@Value和@ConfigurationProperties区别和联系
目錄
?
理論
例子
解決properties文件編碼
?
理論
@ConfigurationProperties注解是把Person加入到容器中,
<bean>標(biāo)簽也是加入到容器中:
<bean class="Person"><property name="lastName" vale="字面量/${key}從環(huán)境變量、配置文件中獲取值/#{spEL}"></property> <bean/>@Value和@ConfigurationProperties為屬性注入值對(duì)比
| ? | @ConfigurationProperties | @Value |
| 功能 | 批量注入配置文件中的屬性 | 一一指定 |
| 松散綁定(松散語(yǔ)法) | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303數(shù)據(jù)校驗(yàn) | 支持 | 不支持 |
| 復(fù)雜類(lèi)型封裝 | 支持 | 不支持 |
?
配置文件yml還是properties他們都能獲取得到值;
?
@ConfigurationProperties
與@Bean結(jié)合為屬性的賦值
與@PropertySource(只能用于properties文件)結(jié)合讀取指定文件
?
屬性名匹配規(guī)則(Relaxed binding)
標(biāo)準(zhǔn)寫(xiě)法:person.firstName
大寫(xiě)用-:person.first-name
大寫(xiě)用_:person.first_name
推薦屬性:PERSON_FIRST_NAME
?
例子
項(xiàng)目結(jié)構(gòu)如下:
源碼如下:
Dog.java
package com.peoperties.peoperties.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.peoperties.peoperties.bean;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.Date; import java.util.List; import java.util.Map;@Component @ConfigurationProperties(prefix = "person") public class Person {//@Value("${person.last-name}")private String lastName;//@Value("#{11*2}")private Integer age;//@Value("true")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;} }PeopertiesApplication.java
package com.peoperties.peoperties;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class PeopertiesApplication {public static void main(String[] args) {SpringApplication.run(PeopertiesApplication.class, args);}}application.properties
server.port=8081#配置person的值 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=15PeopertiesApplicationTests.java
package com.peoperties.peoperties;import com.peoperties.peoperties.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 PeopertiesApplicationTests {@AutowiredPerson person;@Testpublic void contextLoads() {System.out.println(person);}}運(yùn)行截圖如下:
?
?
解決properties文件編碼
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Spring Boot读取peoperties配置及@Value和@ConfigurationProperties区别和联系的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Arduino笔记-ESP8266模块实
- 下一篇: Qt工作笔记-QSplitter的使用(