欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中@Value注解詳細圖文講解

 更新時間:2023年11月27日 11:18:47   作者:迎戰(zhàn)未來  
在spring中有兩種注入方式一種是XML文件注入,另一種則是注解注入,這篇文章主要給大家介紹了關于Spring中@Value注解的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

Spring中的@Value注解詳解

概述

本文配置文件為yml文件

在使用spring框架的項目中,@Value是經(jīng)常使用的注解之一。其功能是將與配置文件中的鍵對應的值分配給其帶注解的屬性。在日常使用中,我們常用的功能相對簡單。本文使您系統(tǒng)地了解@Value的用法。

@Value 注解可以用來將外部的值動態(tài)注入到 Bean 中,在 @Value 注解中,可以使${} 與 #{} ,它們的區(qū)別如下:

(1)@Value(“${}”):可以獲取對應屬性文件中定義的屬性值。

(2)@Value(“#{}”):表示 SpEl 表達式通常用來獲取 bean 的屬性,或者調(diào)用 bean 的某個方法。

使用方式

根據(jù)注入的內(nèi)容來源,@ Value屬性注入功能可以分為兩種:通過配置文件進行屬性注入和通過非配置文件進行屬性注入。

非配置文件注入的類型如下:

  • 注入普通字符串
  • 注入操作系統(tǒng)屬性
  • 注入表達式結果
  • 注入其他bean屬性
  • 注入URL資源

基于配置文件的注入

首先,讓我們看一下配置文件中的數(shù)據(jù)注入,無論它是默認加載的application.yml還是自定義my.yml文檔(需要@PropertySource額外加載)。

application.yml文件配置,獲得里面配置的端口號

程序源代碼

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {

    /**
     *Get in application.yml
     */
    @Value("${server.port}")
    private String port;
    
    @Test
    public  void  getPort(){
       System.out.println(port);
    }
}

程序結果

自定義yml文件,application-config.yml文件配置,獲得里面配置的用戶密碼值

注意,如果想導入自定義的yml配置文件,應該首先把自定義文件在application.yml文件中進行注冊,自定義的yml文件要以application開頭,形式為application-fileName

配置信息

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {
    /**
     *Get in application-config.yml
     */
    @Value("${user.password}")
    private String password;
    
    @Test
    public  void  getPassword(){
       System.out.println(password);
    }
}

程序結果

基于配置文件一次注入多個值

配置信息

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {

    /**
     *Injection array (automatically split according to ",")
     */
    @Value("${tools}")
    private String[] toolArray;
    
    /**
     *Injection list form (automatic segmentation based on "," and)
     */
    @Value("${tools}")
    private List<String> toolList;
    
    @Test
    public  void  getTools(){
       System.out.println(toolArray);
       System.out.println(toolList);
    }
}

程序結果

基于非配置文件的注入

在使用示例說明基于非配置文件注入屬性的實例之前,讓我們看一下SpEl。

Spring Expression Language是Spring表達式語言,可以在運行時查詢和操作數(shù)據(jù)。使用#{…}作為操作符號,大括號中的所有字符均視為SpEl。

讓我們看一下特定實例場景的應用:

注入普通字符串

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {

    // 直接將字符串賦值給 str 屬性
    @Value("hello world")
    private String str;
    
    
    @Test
    public  void  getValue(){
    
        System.out.println(str);
    }	
}

程序結果

注入操作系統(tǒng)屬性

可以利用 @Value 注入操作系統(tǒng)屬性。

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {

    @Value("#{systemProperties['os.name']}")
    private String osName; // 結果:Windows 10
    
    @Test
    public  void  getValue(){
    
        System.out.println(osName);
    }
}

程序結果

注入表達式結果

在 @Value 中,允許我們使用表達式,然后自動計算表達式的結果。將結果復制給指定的變量。如下

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {
    
    // 生成一個隨機數(shù)
    @Value("#{ T(java.lang.Math).random() * 1000.0 }")
    private double randomNumber;
    
    @Test
    public  void  getValue(){
    
        System.out.println(randomNumber);
    }
}

程序結果

注入其他bean屬性

其他Bean

package cn.wideth.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//其他bean,自定義名稱為 myBeans
@Component("myBeans")
public class OtherBean {
    
    @Value("OtherBean的NAME屬性")
    private String name;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {
    
    @Value("#{myBeans.name}")
    private String fromAnotherBean;
    
    @Test
    public  void  getValue(){
    
        System.out.println(fromAnotherBean);
    }
}

程序結果

注入URL資源

測試程序

package cn.wideth.controller;

import cn.wideth.PdaAndIpadApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;

@RunWith(SpringRunner.class)
@SpringBootTest()
@ContextConfiguration(classes = PdaAndIpadApplication.class)
public class ValueController {

    /**
     *注入 URL 資源
     */
    @Value("https://www.baidu.com/")
    private URL homePage;
    
    @Test
    public  void  getValue(){
    
        System.out.println(homePage);
    }
} 

程序結果

總結 

到此這篇關于Spring中@Value注解的文章就介紹到這了,更多相關Spring @Value注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java使用iText生成PDF的步驟和示例

    Java使用iText生成PDF的步驟和示例

    iText 是一個用于創(chuàng)建和處理 PDF 文檔的開源 Java 庫,iText 主要用于生成 PDF 文件,可以將文本、圖像、表格、列表等內(nèi)容添加到 PDF 中,同時支持對 PDF 進行編輯、合并、分割、加密、數(shù)字簽名等操作,本文介紹了Java使用iText生成PDF的步驟和示例
    2024-10-10
  • maven引入kabeja依賴的實現(xiàn)步驟

    maven引入kabeja依賴的實現(xiàn)步驟

    本文主要介紹了maven引入kabeja依賴的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • AndroidQ沙盒機制之分區(qū)存儲適配

    AndroidQ沙盒機制之分區(qū)存儲適配

    這篇文章主要介紹了AndroidQ沙盒機制之分區(qū)存儲適配,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Java下載文件時文件名亂碼問題解決辦法

    Java下載文件時文件名亂碼問題解決辦法

    我最近在開發(fā)時遇到了文件另存為時文件名出現(xiàn)亂碼,在火狐上正常的文件名,在IE中又出現(xiàn)亂碼問題,然后好不容易在IE下調(diào)試好了文件名亂碼問題,在火狐下又出現(xiàn)亂碼,最后終于感覺這樣是能解決了。具體如下:
    2013-04-04
  • Java根據(jù)實體生成SQL數(shù)據(jù)庫表的示例代碼

    Java根據(jù)實體生成SQL數(shù)據(jù)庫表的示例代碼

    這篇文章主要來和大家分享一個Java實現(xiàn)根據(jù)實體生成SQL數(shù)據(jù)庫表的代碼,文中的實現(xiàn)代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-07-07
  • Java?8中的Collectors?API介紹

    Java?8中的Collectors?API介紹

    這篇文章主要介紹了Java?8中的Collectors?API,Stream.collect()是Java?8的流API的終端方法之一。它允許我們對流實例中保存的數(shù)據(jù)元素執(zhí)行可變折疊操作,下文相關內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • 給JavaBean賦默認值并且轉Json字符串的實例

    給JavaBean賦默認值并且轉Json字符串的實例

    這篇文章主要介紹了給JavaBean賦默認值并且轉Json字符串的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot熱部署配置方法詳解

    SpringBoot熱部署配置方法詳解

    在實際開發(fā)中,每次修改代碼就需要重啟項目,重新部署,對于一個后端開發(fā)者來說,重啟確實很難受。在java開發(fā)領域,熱部署一直是一個難以解決的問題,目前java虛擬機只能實現(xiàn)方法體的熱部署,對于整個類的結構修改,仍然需要重啟項目
    2022-11-11
  • JAVA并發(fā)編程有界緩存的實現(xiàn)詳解

    JAVA并發(fā)編程有界緩存的實現(xiàn)詳解

    這篇文章主要介紹了JAVA并發(fā)編程有界緩存的實現(xiàn)詳解的相關資料,這里舉例說明如何實現(xiàn),四種方法一一代碼實現(xiàn),需要的朋友可以參考下
    2016-12-12
  • java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題

    java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題

    這篇文章主要介紹了java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論