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

Spring中的@Value和@PropertySource注解詳解

 更新時(shí)間:2023年11月04日 11:10:36   作者:大樹下躲雨  
這篇文章主要介紹了Spring中的@Value和@PropertySource注解詳解,@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中,本文提供了部分實(shí)現(xiàn)代碼,需要的朋友可以參考下

 一、@Value和@PropertySource

1、@Value

@Value注解:為屬性賦值

賦值方式:

  • 基本數(shù)值
  • SpEl表達(dá)式 #{}
  • ${},讀取配置文件[xxx.properties]中的值,配合注解@PropertySource使用
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
   /**
    * The actual value expression such as <code>#{systemProperties.myProp}</code>
    * or property placeholder such as <code>${my.app.myProp}</code>.
    */
   String value();
}

2、@PropertySource

@PropertySource:讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
   String name() default "";
   String[] value();
   boolean ignoreResourceNotFound() default false;
   String encoding() default "";
   Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

二、@Value和@PropertySource案例

1、項(xiàng)目結(jié)構(gòu)

在這里插入圖片描述

2、Person

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
    /**
     * @Value賦值方式1:基本數(shù)值
     */
    @Value("張三")
    private String name;
    /**
     * @Value賦值方式2:SpEl表達(dá)式  #{}
     */
    @Value("#{ 20-2 }")
    private int age;
    /**
     * @Value賦值方式3:${},讀取配置文件[xxx.properties]中的值
     */
    @Value("${person.phone}")
    private String phone;
    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 String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                '}';
    }
}

3、配置類

import org.springframework.context.annotation.*;

/**
 * 使用@PropertySource注解可以讀取外部配置文件中的key-value保存到運(yùn)行的環(huán)境變量中
 */
@PropertySource(value = {"classpath:/person.properties"},encoding = "UTF-8")
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
}

4、外部文件person.properties

person.phone = 11111111

5、測(cè)試類

import com.dashu.bean.Person;
import com.dashu.config.BeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class Main {
    public static void main(String[] args) {
        //加載配置類獲取容器
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        //容器中獲取Person
        Person person = annotationConfigApplicationContext.getBean(Person.class);
        //打印
        System.out.println(person);
        System.out.println("--------------------");
        /**
         * 獲取運(yùn)行時(shí)環(huán)境實(shí)例
         */
        ConfigurableEnvironment environment = annotationConfigApplicationContext.getEnvironment();
        /**
         * 根據(jù)外部文件的key,從環(huán)境中獲取value
         */
        String property = environment.getProperty("person.phone");
        //打印
        System.out.println(property);
    }
}

6、測(cè)試結(jié)果

在這里插入圖片描述

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

相關(guān)文章

  • JAVA springboot如何開啟倆端口

    JAVA springboot如何開啟倆端口

    這篇文章主要介紹了JAVA springboot如何開啟倆端口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java由淺入深帶你精通繼承super

    Java由淺入深帶你精通繼承super

    繼承就是子類繼承父類的特征和行為,使得子類對(duì)象(實(shí)例)具有父類的實(shí)例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為
    2022-03-03
  • Java實(shí)現(xiàn)abc字符串排列組合

    Java實(shí)現(xiàn)abc字符串排列組合

    這篇文章主要為大家詳細(xì)介紹了JAVA實(shí)現(xiàn)abc字符串的排列組合,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑

    詳談Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑

    下面小編就為大家分享一篇Java中net.sf.json包關(guān)于JSON與對(duì)象互轉(zhuǎn)的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-12-12
  • Java多線程Semaphore工具的使用詳解

    Java多線程Semaphore工具的使用詳解

    Semaphore 是一種用于控制線程并發(fā)訪問數(shù)的同步工具。它通過維護(hù)一定數(shù)量的許可證來限制對(duì)共享資源的訪問,許可證的數(shù)量就是可以同時(shí)訪問共享資源的線程數(shù)目,需要的朋友可以參考下
    2023-05-05
  • 詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)

    詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)

    本篇文章主要介紹了詳解Spring boot Admin 使用eureka監(jiān)控服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • 詳解maven的install的作用

    詳解maven的install的作用

    這篇文章主要介紹了詳解maven的install的作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)

    SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)

    在目前的企業(yè)級(jí)應(yīng)用開發(fā)中,前后端分離是趨勢(shì),但是視圖層技術(shù)還占有一席之地。Spring Boot 對(duì)視圖層技術(shù)提供了很好的支持,福安防推薦使用的模板引擎是Thymeleaf,不過想FreeMarker也支持,JSP技術(shù)在這里并不推薦使用
    2022-08-08
  • 如何修改覆蓋spring boot默認(rèn)日志策略logback詳解

    如何修改覆蓋spring boot默認(rèn)日志策略logback詳解

    這篇文章主要給大家介紹了關(guān)于如何修改覆蓋spring boot默認(rèn)日志策略logback的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Java實(shí)現(xiàn)8種排序算法的示例代碼

    Java實(shí)現(xiàn)8種排序算法的示例代碼

    這篇文章主要介紹了8種JAVA實(shí)現(xiàn)排序算法的示例代碼,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論

平泉县| 屯昌县| 祁连县| 扬州市| 鄂托克前旗| 通海县| 景泰县| 凌源市| 靖安县| 广宁县| 蕉岭县| 五河县| 抚州市| 武清区| 黔西| 双江| 崇信县| 花莲市| 老河口市| 东平县| 遂川县| 安图县| 资溪县| 贺兰县| 牟定县| 绥阳县| 万年县| 东宁县| 昌吉市| 科技| 铜川市| 交城县| 漯河市| 丰镇市| 永胜县| 扶余县| 台南市| 南城县| 神农架林区| 襄垣县| 襄樊市|