@value和@PropertySource注解怎么在Spring中使用

本篇文章給大家分享的是有關(guān)@value和@PropertySource注解怎么在Spring中使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

10年的道縣網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整道縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“道縣網(wǎng)站設(shè)計”,“道縣網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

  • @Value注解:可以使用注入基本字符串 EL表達式,從配置文件讀取數(shù)據(jù)

  • @PropertySource用于引入單個配置文件

  • @PropertySources用于引入多個配置文件

  • @PropertySource或者@PropertySources引入的數(shù)據(jù)都是存在環(huán)境變量ConfigurableEnvironment中的

  • 在resouces文件夾下新建一個cat.properties文件,寫入內(nèi)容parent=tiger

public class Cat {
  @Value("貓") //直接注入字符串
  private String name;
  @Value("#{12+2}") //支持EL表達式
  private int age;
  @Value("${parent}") //支持配置文件讀取
  private String parent;
  public Cat() {
    System.out.println("貓被初始化");
  }

  @Override
  public String toString() {
    return "Cat{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", parent='" + parent + '\'' +
        '}';
  }
}
@Import({Cat.class})
@PropertySources({@PropertySource(value ="cat.properties")})
public class Appconfig {
}
public class Demo {
  public static void main(String[] args) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Appconfig.class);
    Cat bean = context.getBean(Cat.class);
    System.out.println(bean); //Cat{name='貓', age=14, parent='tiger'}
    ConfigurableEnvironment environment = context.getEnvironment();
    System.out.println(environment.getProperty("parent")); //tiger


  }
}

@value和@PropertySource注解怎么在Spring中使用

以上就是@value和@PropertySource注解怎么在Spring中使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

標題名稱:@value和@PropertySource注解怎么在Spring中使用
文章位置:http://bm7419.com/article16/jjdjdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、營銷型網(wǎng)站建設(shè)網(wǎng)站制作、面包屑導(dǎo)航、App開發(fā)、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)