最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中@Value注解詳細(xì)圖文講解

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

Spring中的@Value注解詳解

概述

本文配置文件為yml文件

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

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

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

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

使用方式

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

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

  • 注入普通字符串
  • 注入操作系統(tǒng)屬性
  • 注入表達(dá)式結(jié)果
  • 注入其他bean屬性
  • 注入U(xiǎn)RL資源

基于配置文件的注入

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

application.yml文件配置,獲得里面配置的端口號(hào)

程序源代碼

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);
    }
}

程序結(jié)果

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

注意,如果想導(dǎo)入自定義的yml配置文件,應(yīng)該首先把自定義文件在application.yml文件中進(jìn)行注冊(cè),自定義的yml文件要以application開(kāi)頭,形式為application-fileName

配置信息

測(cè)試程序

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);
    }
}

程序結(jié)果

基于配置文件一次注入多個(gè)值

配置信息

測(cè)試程序

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);
    }
}

程序結(jié)果

基于非配置文件的注入

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

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

讓我們看一下特定實(shí)例場(chǎng)景的應(yīng)用:

注入普通字符串

測(cè)試程序

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);
    }	
}

程序結(jié)果

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

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

測(cè)試程序

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; // 結(jié)果:Windows 10
    
    @Test
    public  void  getValue(){
    
        System.out.println(osName);
    }
}

程序結(jié)果

注入表達(dá)式結(jié)果

在 @Value 中,允許我們使用表達(dá)式,然后自動(dòng)計(jì)算表達(dá)式的結(jié)果。將結(jié)果復(fù)制給指定的變量。如下

測(cè)試程序

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 {
    
    // 生成一個(gè)隨機(jī)數(shù)
    @Value("#{ T(java.lang.Math).random() * 1000.0 }")
    private double randomNumber;
    
    @Test
    public  void  getValue(){
    
        System.out.println(randomNumber);
    }
}

程序結(jié)果

注入其他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;
    }
}

測(cè)試程序

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);
    }
}

程序結(jié)果

注入U(xiǎn)RL資源

測(cè)試程序

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);
    }
} 

程序結(jié)果

總結(jié) 

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

相關(guān)文章

  • java設(shè)計(jì)模式之簡(jiǎn)單工廠模式簡(jiǎn)述

    java設(shè)計(jì)模式之簡(jiǎn)單工廠模式簡(jiǎn)述

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之簡(jiǎn)單工廠模式,簡(jiǎn)單工廠模式的實(shí)質(zhì)是由一個(gè)工廠類根據(jù)傳入的參數(shù),動(dòng)態(tài)決定應(yīng)該創(chuàng)建哪一個(gè)產(chǎn)品類的實(shí)例,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Java通過(guò)反射來(lái)打印類的方法實(shí)現(xiàn)

    Java通過(guò)反射來(lái)打印類的方法實(shí)現(xiàn)

    本文主要介紹了Java通過(guò)反射來(lái)打印類的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 基于springboot+enum配置化的方法

    基于springboot+enum配置化的方法

    本文主要介紹利用Springboot結(jié)合枚舉類enum進(jìn)行自定義參數(shù)的初始化和應(yīng)用,通過(guò)@Value注解實(shí)現(xiàn)參數(shù)的動(dòng)態(tài)注入,以實(shí)現(xiàn)靈活可維護(hù)的配置管理,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • Java鎖之可重入鎖介紹

    Java鎖之可重入鎖介紹

    這篇文章主要介紹了Java鎖之可重入鎖介紹,可重入鎖,也叫做遞歸鎖,指的是同一線程外層函數(shù)獲得鎖之后,內(nèi)層遞歸函數(shù)仍然有獲取該鎖的代碼,但不受影響,需要的朋友可以參考下
    2014-11-11
  • java利用遞歸算法實(shí)現(xiàn)對(duì)文件夾的刪除功能

    java利用遞歸算法實(shí)現(xiàn)對(duì)文件夾的刪除功能

    這篇文章主要介紹了java利用遞歸算法實(shí)現(xiàn)對(duì)文件夾的刪除功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • java導(dǎo)出pdf文件的詳細(xì)實(shí)現(xiàn)方法

    java導(dǎo)出pdf文件的詳細(xì)實(shí)現(xiàn)方法

    這篇文章主要介紹了java導(dǎo)出pdf文件的詳細(xì)實(shí)現(xiàn)方法,包括制作模板、獲取中文字體文件、實(shí)現(xiàn)后端服務(wù)以及前端發(fā)起請(qǐng)求并生成下載鏈接,需要的朋友可以參考下
    2025-03-03
  • javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳

    javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳

    這篇文章主要為大家詳細(xì)介紹了JAVAWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java?axios與spring前后端分離傳參規(guī)范總結(jié)

    Java?axios與spring前后端分離傳參規(guī)范總結(jié)

    這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Java編程接口回調(diào)一般用法代碼解析

    Java編程接口回調(diào)一般用法代碼解析

    本文的主要內(nèi)容是同過(guò)實(shí)際代碼向大家展示Java編程中接口回調(diào)的一般用法,具有一定參考價(jià)值,需要的朋友可以了解下
    2017-09-09
  • java音樂(lè)播放器課程設(shè)計(jì)

    java音樂(lè)播放器課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了java音樂(lè)播放器的課程設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論

牟定县| 武清区| 平江县| 河间市| 阳泉市| 阿拉善左旗| 梨树县| 芮城县| 勐海县| 平江县| 杨浦区| 乌兰县| 金阳县| 上虞市| 曲周县| 屏东县| 茂名市| 安图县| 鲁甸县| 三门县| 灵武市| 郑州市| 香港| 油尖旺区| 永仁县| 靖江市| 策勒县| 泰宁县| 高台县| 宜兴市| 宜良县| 新巴尔虎左旗| 班玛县| 夹江县| 屯门区| 鲁山县| 柯坪县| 梅河口市| 大渡口区| 石景山区| 广南县|