組合使用,可以將自定義屬性文件中的屬性變量值注入到當(dāng)前類的使用@Value注解的成員變量中,需要的朋友可以參考下" />

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

Spring中@PropertySource配置的用法

 更新時(shí)間:2023年11月23日 09:12:37   作者:馬爾斯的藍(lán)色  
這篇文章主要介紹了Spring中@PropertySource配置的用法,@PropertySource 和 @Value
組合使用,可以將自定義屬性文件中的屬性變量值注入到當(dāng)前類的使用@Value注解的成員變量中,需要的朋友可以參考下

@PropertySource配置的用法

功能

  • 加載指定的屬性文件(*.properties)到 Spring 的 Environment 中??梢耘浜?@Value 和 @ConfigurationProperties 使用。
  • @PropertySource 和 @Value組合使用,可以將自定義屬性文件中的屬性變量值注入到當(dāng)前類的使用@Value注解的成員變量中。
  • @PropertySource 和 @ConfigurationProperties組合使用,可以將屬性文件與一個(gè)Java類綁定,將屬性文件中的變量值注入到該Java類的成員變量中。

源碼

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.io.support.PropertySourceFactory;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {

    /**
     * 屬性源的名稱
     */
    String name() default "";

    /**
     * 屬性文件的存放路徑
     */
    String[] value();

    /**
     * 如果指定的屬性源不存在,是否要忽略這個(gè)錯(cuò)誤
     */
    boolean ignoreResourceNotFound() default false;

    /**
     * 屬性源的編碼格式
     */
    String encoding() default "";

    /**
     * 屬性源工廠
     */
    Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;

}

使用示例

屬性文件:demo.properties

demo.name=huang
demo.sex=1
demo.type=demo

示例一:@PropertySource + @Value

package com.huang.pims.demo.props;

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

@Component
@PropertySource(value = {"demo/props/demo.properties"})
public class ReadByPropertySourceAndValue {

    @Value("${demo.name}")
    private String name;

    @Value("${demo.sex}")
    private int sex;

    @Value("${demo.type}")
    private String type;

    @Override
    public String toString() {
        return "ReadByPropertySourceAndValue{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", type='" + type + '\'' +
                '}';
    }
}

示例二:@PropertySource 和 @ConfigurationProperties

package com.huang.pims.demo.props;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {"demo/props/demo.properties"})
@ConfigurationProperties(prefix = "demo")
public class ReadByPropertySourceAndConfProperties {

    private String name;

    private int sex;

    private String type;

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public int getSex() {
        return sex;
    }

    public String getType() {
        return type;
    }

    @Override
    public String toString() {
        return "ReadByPropertySourceAndConfProperties{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", type='" + type + '\'' +
                '}';
    }
}

示例測(cè)試

package com.huang.pims.demo.runners;

import com.huang.pims.demo.props.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class OutputPropsRunner implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(OutputPropsRunner.class);

    @Autowired
    private ReadByPropertySourceAndValue readByPropertySourceAndValue;

    @Autowired
    private ReadByPropertySourceAndConfProperties readByPropertySourceAndConfProperties;


    @Override
    public void run(String... args) throws Exception {
        LOGGER.info(readByPropertySourceAndValue.toString());
        LOGGER.info(readByPropertySourceAndConfProperties.toString());
    }

}

啟動(dòng)項(xiàng)目即可看到效果。

在這里插入圖片描述

從截圖中可以看出,需要讀取的屬性配置,都已經(jīng)成功讀取出來了。

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

相關(guān)文章

  • Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之時(shí)間復(fù)雜度與空間復(fù)雜度

    Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之時(shí)間復(fù)雜度與空間復(fù)雜度

    對(duì)于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會(huì)使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲(chǔ)空間,這篇文章主要給大家介紹了關(guān)于Java時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實(shí)現(xiàn)方式

    java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實(shí)現(xiàn)方式

    近期項(xiàng)目有個(gè)需求,需要將組織機(jī)構(gòu)數(shù)據(jù)拼成樹型結(jié)構(gòu)返回至前端,下面這篇文章主要給大家介紹了關(guān)于java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實(shí)現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • idea如何配置springboot熱部署

    idea如何配置springboot熱部署

    這篇文章主要介紹了idea如何配置springboot熱部署問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • springboot實(shí)現(xiàn)啟動(dòng)直接訪問項(xiàng)目地址

    springboot實(shí)現(xiàn)啟動(dòng)直接訪問項(xiàng)目地址

    這篇文章主要介紹了springboot實(shí)現(xiàn)啟動(dòng)直接訪問項(xiàng)目地址,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring data jpa @Query update的坑及解決

    Spring data jpa @Query update的坑及解決

    這篇文章主要介紹了Spring data jpa @Query update的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列

    Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之棧和隊(duì)列,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-05-05
  • SpringBoot Bean花式注解方法示例下篇

    SpringBoot Bean花式注解方法示例下篇

    這篇文章主要介紹了SpringBoot Bean花式注解方法,很多時(shí)候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個(gè)Bean
    2023-02-02
  • Java線程協(xié)作的兩種方式小結(jié)

    Java線程協(xié)作的兩種方式小結(jié)

    Java中線程協(xié)作的最常見的兩種方式是利用Object.wait()、Object.notify()和使用Condition,本文主要介紹了Java線程協(xié)作的兩種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java重載構(gòu)造原理與用法詳解

    Java重載構(gòu)造原理與用法詳解

    這篇文章主要介紹了Java重載構(gòu)造原理與用法,結(jié)合實(shí)例形式分析了java可變參數(shù)、方法重載、構(gòu)造器等相關(guān)概念、原理及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • RocketMQ?producer發(fā)送者淺析

    RocketMQ?producer發(fā)送者淺析

    RocketMQ生產(chǎn)者是一種高性能、可靠的消息發(fā)送者,能夠?qū)⑾⒖焖?、可靠地發(fā)送到RocketMQ消息隊(duì)列中。它具有多種消息發(fā)送模式和消息發(fā)送方式,可以根據(jù)不同的業(yè)務(wù)需求進(jìn)行靈活配置
    2023-04-04

最新評(píng)論

盐池县| 灵台县| 天镇县| 万全县| 马龙县| 秦皇岛市| 红原县| 探索| 宣武区| 精河县| 哈密市| 宜川县| 铁力市| 平利县| 大田县| 伽师县| 铁力市| 丹江口市| 沭阳县| 乡城县| 岢岚县| 武平县| 潮安县| 连平县| 习水县| 临澧县| 许昌县| 铅山县| 汉寿县| 怀远县| 大安市| 吕梁市| 延川县| 金乡县| 金坛市| 元江| 横山县| 井冈山市| 久治县| 商城县| 武定县|