国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring中的@Value注解詳解

這篇具有很好參考價值的文章主要介紹了Spring中的@Value注解詳解。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Spring中的@Value注解詳解

概述

本文配置文件為yml文件

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

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

(1)@Value(“${}”):可以獲取對應(yīng)屬性文件中定義的屬性值。
(2)@Value(“#{}”):表示 SpEl 表達式通常用來獲取 bean 的屬性,或者調(diào)用 bean 的某個方法。

使用方式

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

  1. 注入普通字符串
  2. 注入操作系統(tǒng)屬性
  3. 注入表達式結(jié)果
  4. 注入其他bean屬性
  5. 注入URL資源
基于配置文件的注入

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

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

Spring中的@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 {

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

程序結(jié)果

Spring中的@Value注解詳解

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

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

Spring中的@Value注解詳解

配置信息

Spring中的@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 {
    /**
     *Get in application-config.yml
     */
    @Value("${user.password}")
    private String password;
    
    @Test
    public  void  getPassword(){
       System.out.println(password);
    }
}

程序結(jié)果

Spring中的@Value注解詳解

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

配置信息

Spring中的@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;

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é)果

Spring中的@Value注解詳解

基于非配置文件的注入

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

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

讓我們看一下特定實例場景的應(yī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;

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é)果

Spring中的@Value注解詳解

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

程序結(jié)果

Spring中的@Value注解詳解

注入表達式結(jié)果

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

測試程序

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

程序結(jié)果

Spring中的@Value注解詳解

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

程序結(jié)果

Spring中的@Value注解詳解

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

程序結(jié)果

Spring中的@Value注解詳解文章來源地址http://www.zghlxwxcb.cn/news/detail-458141.html

到了這里,關(guān)于Spring中的@Value注解詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring 中的 @ConditionalOnProperty 注解

    介紹@ConditionalOnProperty注解的主要目的。 通常,在開發(fā)基于Spring的應(yīng)用程序時,可能需要根據(jù)配置屬性的存在和值有條件地創(chuàng)建一些bean。 例如,取決于是否將屬性值設(shè)置為“ prod”或“ test”,可能想要注冊一個DataSource bean來指向生產(chǎn)或測試數(shù)據(jù)庫。 幸運的是,實現(xiàn)這一目標(biāo)

    2024年02月16日
    瀏覽(21)
  • Spring中的注解

    Spring的配置 spring 2.5前==xml spring 2.5后==xml+annotation spring 3.0后==annotation+JavaConfig配置類 注解: 1.注入類 替換:bean id=\\\"\\\" class=\\\"\\\"/bean 位置:類 語法:@Component(value=\\\"注入容器中的id,如果省略id為類名且首字母小寫,value屬性名稱可以省略\\\") eg: bean id=\\\"user\\\" class=\\\"com.apesource.包.User\\\"/bean ?

    2024年01月20日
    瀏覽(19)
  • Spring中的自定義注解

    在Spring中,注解是一種非常使用的工具。 因其強大的功能,極大的提高了我們開發(fā)效率。 但是當(dāng)遇到一些特殊業(yè)務(wù)時,框架自有的注解已經(jīng)不能滿足我們的需求了,這時我們就可以添加自定義注解來滿足我們的業(yè)務(wù)需求。 我們用 @interface 來聲明這是一個注解類。 另外需要在

    2024年02月11日
    瀏覽(17)
  • Spring Boot 中的 @EnableDiscoveryClient 注解

    Spring Boot 中的 @EnableDiscoveryClient 注解

    Spring Boot 是一個快速開發(fā) Spring 應(yīng)用程序的框架,它提供了一些基礎(chǔ)設(shè)施,使得我們可以快速地開發(fā)出高效、可靠的應(yīng)用程序。其中,@EnableDiscoveryClient 注解是 Spring Boot 中一個非常重要的注解,它提供了一種便捷的方式來將 Spring Boot 應(yīng)用程序注冊到服務(wù)注冊中心中。本文將介

    2024年02月12日
    瀏覽(19)
  • Spring Boot 中的 @Cacheable 注解

    Spring Boot 中的 @Cacheable 注解

    在 Spring Boot 中,緩存是一個非常重要的話題。當(dāng)我們需要頻繁讀取一些數(shù)據(jù)時,為了提高性能,可以將這些數(shù)據(jù)緩存起來,避免每次都從數(shù)據(jù)庫中讀取。為了實現(xiàn)緩存,Spring Boot 提供了一些緩存注解,其中最常用的是 @Cacheable 注解。 @Cacheable 注解用于標(biāo)記一個方法需要被緩存

    2024年02月12日
    瀏覽(37)
  • Spring Boot 中的 @CacheEvict 注解

    Spring Boot 中的 @CacheEvict 注解

    在 Spring Boot 中,緩存是提高應(yīng)用性能的重要手段。為了更好地管理緩存,Spring Boot 提供了一系列的緩存注解,其中 @CacheEvict 注解用于清空緩存。 本文將介紹 @CacheEvict 注解的含義、原理以及如何使用。 @CacheEvict 注解用于清空緩存。它可以標(biāo)注在方法上,表示在執(zhí)行該方法后

    2024年02月09日
    瀏覽(18)
  • Spring Boot中的@EnableAutoConfiguration注解

    Spring Boot中的@EnableAutoConfiguration注解

    Spring Boot是一個非常流行的Java框架,它可以快速創(chuàng)建基于Spring的應(yīng)用程序。Spring Boot提供了許多自動配置功能,使得開發(fā)者可以非常容易地創(chuàng)建一個可運行的應(yīng)用程序。其中,@EnableAutoConfiguration注解是Spring Boot自動配置功能的核心之一。 @EnableAutoConfiguration注解是Spring Boot的核心

    2024年02月11日
    瀏覽(18)
  • Spring Boot 中的 @HystrixCommand 注解

    Spring Boot 中的 @HystrixCommand 注解

    在分布式系統(tǒng)中,服務(wù)之間的調(diào)用是不可避免的。但隨著服務(wù)數(shù)量的增加,服務(wù)之間的依賴關(guān)系也會變得越來越復(fù)雜,服務(wù)的故障也會變得越來越常見。一旦某個服務(wù)出現(xiàn)故障,它所依賴的服務(wù)也會受到影響,導(dǎo)致整個系統(tǒng)出現(xiàn)故障。為了應(yīng)對這種情況,Netflix 開發(fā)了 Hystri

    2024年02月17日
    瀏覽(22)
  • Spring框架中的@Conditional系列注解

    Spring框架中的@Conditional系列注解

    Conditional 是由SpringFramework提供的一個注解,位于 org.springframework.context.annotation 包內(nèi),定義如下。 SpringBoot 模塊大量的使用@Conditional 注釋,我們可以將Spring的@Conditional注解用于以下場景: 可以作為類級別的注解直接或者間接的與@Component相關(guān)聯(lián),包括@Configuration類; 可以作為元

    2024年02月08日
    瀏覽(62)
  • Spring MVC中的一些常用注解

    Spring MVC中的一些常用注解

    目錄 @RequestMapping 實現(xiàn)路由映射 限制請求方式 @PathVariable 從url中獲取變量的值 更改綁定參數(shù)的名字 @RequestParam 可以傳遞集合? 更改綁定參數(shù)的名字 可修改是否為必傳參數(shù) @RequestBody 獲取請求正文的內(nèi)容? 可修改是否為必傳參數(shù) @RequestPart 可以支持上傳文件 更改綁定參數(shù)的名字

    2024年01月19日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包