国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot 如何從配置文件讀取值到對象中

SpringBoot 如何從配置文件讀取值到對象中

2022-03-09 00:37無暇淺安時光 Java教程

這篇文章主要介紹了SpringBoot 如何從配置文件讀取值到對象中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、實現方式

@ConfigurationProperties 注解

(最好加上前綴prefix=“person”,標明是和配置文件中哪個開頭的屬性匹配)

推薦使用用在類上,從配置文件讀取屬性值,放到對象里面,復雜的結構也適用例如map,list,對象。支持校驗:@Validated

@Valid注解

用在屬性上,需要每個屬性逐個綁定通過@value注解獲取配置文件的值,不適合做復雜類型(map,list ,對象)值得獲取不支持@Validated

 

二、兩者區別

SpringBoot 如何從配置文件讀取值到對象中

 

三、代碼演示

使用@ConfigurationProperties注解

package com.wx.springboot20190911.demo.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 1.ConfigurationProperties注解 從配置文件讀取屬性值,放到對象里面
* 2.通過@value注解獲取配置文件的值
*/
@Component//perosn 需要納入spring ioc 容器里
@ConfigurationProperties(prefix = "person")//使用前綴標明具體的屬性
@Validated
public class Person {
  @Email
  String email;
  String hello;
  String name;
  int age;
  boolean boss;
  Date birth;
  Map<String,String> maps;
  List<String> list;
  Dog dog;
  @Override
  public String toString() {
      return "Person{" +
              "email='" + email + '\'' +
              ", hello='" + hello + '\'' +
              ", name='" + name + '\'' +
              ", age=" + age +
              ", boss=" + boss +
              ", birth=" + birth +
              ", maps=" + maps +
              ", list=" + list +
              ", dog=" + dog +
              '}';
  }
  public String getEmail() {
      return email;
  }
  public void setEmail(String email) {
      this.email = email;
  }
  public String getHello() {
      return hello;
  }
  public void setHello(String hello) {
      this.hello = hello;
  }
  public Dog getDog() {
      return dog;
  }
  public void setDog(Dog dog) {
      this.dog = dog;
  }
  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 boolean isBoss() {
      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, String> getMaps() {
      return maps;
  }
  public void setMaps(Map<String, String> maps) {
      this.maps = maps;
  }
  public List<String> getList() {
      return list;
  }
  public void setList(List<String> list) {
      this.list = list;
  }
}
package com.wx.springboot20190911.demo.model;
public class Dog {
  String name;
  String color;
  int age;
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getColor() {
      return color;
  }
  public void setColor(String color) {
      this.color = color;
  }
  public int getAge() {
      return age;
  }
  public void setAge(int age) {
      this.age = age;
  }
  @Override
  public String toString() {
      return "Dog{" +
              "name='" + name + '\'' +
              ", color='" + color + '\'' +
              ", age=" + age +
              '}';
  }
}

使用@Value注解

package com.wx.springboot20190911.demo.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 1.ConfigurationProperties注解 從配置文件讀取屬性值,放到對象里面,推薦使用,復雜的結構也適用例如map,list對象,
* 支持 校驗@Validated
* 2.通過@value注解獲取配置文件的值,不適合做復雜類型值得獲取,不支持@Validated,支持
*/
@Component//perosn 需要納入spring ioc 容器里
//@ConfigurationProperties(prefix = "person")
@Validated
public class Person1 {
  @Email
  @Value("${person1.email}")
  String email;
  @Value("${person1.hello}")
  String hello;
  @Value("${person1.name}")
  String name;
  @Value("#{12*3}")//支持計算
  int age;
  @Value("${person1.boss}")
  boolean boss;
  @Value("${person1.birth}")
  Date birth;
  Map<String,String> maps;
  List<String> list;
  Dog dog;
  public String getHello() {
      return hello;
  }
  public void setHello(String hello) {
      this.hello = hello;
  }
  public Dog getDog() {
      return dog;
  }
  public void setDog(Dog dog) {
      this.dog = dog;
  }
  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 boolean isBoss() {
      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, String> getMaps() {
      return maps;
  }
  public void setMaps(Map<String, String> maps) {
      this.maps = maps;
  }
  public List<String> getList() {
      return list;
  }
  public void setList(List<String> list) {
      this.list = list;
  }
  public String getEmail() {
      return email;
  }
  public void setEmail(String email) {
      this.email = email;
  }
  @Override
  public String toString() {
      return "Person1{" +
              "email='" + email + '\'' +
              ", hello='" + hello + '\'' +
              ", name='" + name + '\'' +
              ", age=" + age +
              ", boss=" + boss +
              ", birth=" + birth +
              ", maps=" + maps +
              ", list=" + list +
              ", dog=" + dog +
              '}';
  }
}

配置類(application.properties)代碼

person.hello=luck
person.name=吳
#亂碼的話 就setting設置下file encoding
person.list=ww,xx,rr
person.maps.k1=v1
person.maps.k2=v2
person.dog.name=cat
person.dog.color=red
person.dog.age=1
person.age=12
person.birth=2019/01/11
person.boss=false
person.email=www@qq.com
person1.hello=luck
person1.name=吳
#亂碼的話 就setting設置下file encoding
person1.list=ww,xx,rr
person1.age=12
person1.birth=2019/01/11
person1.boss=false
person1.email=www

配置類(application.yml)代碼:這種方式更加結構化

person:
name: 霞
age: 16
boss : false
birth: 2012/09/12
maps: {k1: v1,k2: v2}
list: [dog,cat ,house,rabbits]
dog:
  name: ${person.hello}
  age: ${random.int(10)}
  color: white
hello: yula

打印結果:使用第一種方式時,如果email不是"www@qq.com"這種格式,是不能運行成功的,但是使用@Value 不會校驗,如下面是"www",一樣能運行成功

Person{email='www@qq.com', hello='luck', name='吳', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吳', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

 

四、@PropertySource 讀取指定配置文件

package com.wx.springboot20190911.demo.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 1.ConfigurationProperties注解 從配置文件讀取屬性值,放到對象里面
* 2.通過@value注解獲取配置文件的值
*/
@PropertySource(value={"classpath:person.properties"})//指定讀取person.properties配置文件
@Component//perosn 需要納入spring ioc 容器里
@ConfigurationProperties(prefix = "person")//使用前綴標明具體的屬性
@Validated
public class Person2 {
  @Email
  String email;
  String hello;
  String name;
  int age;
  boolean boss;
  Date birth;
  Map<String,String> maps;
  List<String> list;
  Dog dog;
  @Override
  public String toString() {
      return "Person{" +
              "email='" + email + '\'' +
              ", hello='" + hello + '\'' +
              ", name='" + name + '\'' +
              ", age=" + age +
              ", boss=" + boss +
              ", birth=" + birth +
              ", maps=" + maps +
              ", list=" + list +
              ", dog=" + dog +
              '}';
  }
  public String getEmail() {
      return email;
  }
  public void setEmail(String email) {
      this.email = email;
  }
  public String getHello() {
      return hello;
  }
  public void setHello(String hello) {
      this.hello = hello;
  }
  public Dog getDog() {
      return dog;
  }
  public void setDog(Dog dog) {
      this.dog = dog;
  }
  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 boolean isBoss() {
      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, String> getMaps() {
      return maps;
  }
  public void setMaps(Map<String, String> maps) {
      this.maps = maps;
  }
  public List<String> getList() {
      return list;
  }
  public void setList(List<String> list) {
      this.list = list;
  }
}

這里雖然指定了讀取person.properties配置文件,但是由于prefix=“person”,導致還是讀取了application.properties配置文件,因為application.properties權限最高,要想讀取person.properties配置文件,就得改前綴名,例如改成prefix=“person2” person.properties配置文件的內容如下:

SpringBoot 如何從配置文件讀取值到對象中

沒改前綴名的結果演示:

Person{email='www@qq.com', hello='luck', name='吳', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吳', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

Person{email='www@qq.com', hello='luck', name='吳', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

改前綴名的代碼演示:

SpringBoot 如何從配置文件讀取值到對象中 SpringBoot 如何從配置文件讀取值到對象中

改了前綴名的結果演示:這時候讀取的就是指定的配置文件的值

Person{email='www@qq.com', hello='luck', name='吳', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吳', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

Person{email='12345@qq.com', hello='springboot', name='指定讀取配置文件', age=12, boss=false, birth=Wed Sep 11 00:00:00 CST 2019, maps={k2=v2, k1=v1}, list=[@Value, @PropertySource], dog=Dog{name='cat', color='red', age=1}}

注:只能讀取 .properties 文件,無法讀取 .yml 文件

 

五、@ImportResource:導入Spring配置文件

讓配置文件里面的內容生效

Springboot 里沒有Spring 的配置文件,我們自己編寫的配置文件,也不能自動識別;

想讓Spring的配置文件生效,加載進來,需使用該注解

寫一個Person3 類,如圖所示:

SpringBoot 如何從配置文件讀取值到對象中

自定義一個Spring配置文件,如圖所示:

SpringBoot 如何從配置文件讀取值到對象中

在啟動類上注解

SpringBoot 如何從配置文件讀取值到對象中

測試:自定義的這個配置文件是否生效

SpringBoot 如何從配置文件讀取值到對象中

演示結果:生效了

2019-09-11 22:15:12.247 INFO 7824 --- [ main] c.w.s.demo.DemoApplicationTests : Started DemoApplicationTests in 8.558 seconds (JVM running for 10.882)

Person{email='null', hello='null', name='null', age=0, boss=false, birth=null, maps=null, list=null, dog=null}

 

六、思維導圖

SpringBoot 如何從配置文件讀取值到對象中

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/baidu_35160588/article/details/100749171

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品中文在线 | 成人在线观看免费视频 | 成人在线观看免费 | 国产精品成人在线观看 | 久久亚洲天堂 | 欧美日韩在线免费观看 | 国产电影一区二区 | 国产精品久久久久久久久久久小说 | 精品亚洲成a人在线观看 | 欧美日韩精品一区二区三区 | 欧美影 | 正在播放国产一区 | 综合导航 | 国产伦精品一区二区三区四区视频 | 国产视频一二三区 | 超碰天天 | 91网在线| 最近2018年手机中文字幕版 | 日日爱视频 | 欧美精品v国产精品v日韩精品 | 久久99精品久久久久 | 精品视频第一页 | 龙珠z普通话国语版在线观看 | 亚洲国产精品网站 | 欧美成人精精品一区二区频 | 免费在线观看黄色av | 综合婷婷| 91极品视频在线观看 | 亚洲精品一区二区三区蜜桃久 | 黄色片网站在线免费观看 | 日韩中文一区二区三区 | 久久久久久成人 | 国产原创精品视频 | 黄瓜av | 亚洲成人三级 | 久久精品国产久精国产 | 91久久精品国产91久久 | 黄在线免费观看 | av片免费| 亚洲在线 | 自拍偷拍专区 |