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

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

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 + '\'' +
                '}';
    }
}

示例測試

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中使用opencv的問題

    Java中使用opencv的問題

    這篇文章主要介紹了Java中使用opencv的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Java 中 getClass() 方法的使用與原理深入分析(對象類型信息)

    Java 中 getClass() 方法的使用與原理深入分析(對象類型信息)

    在 Java 編程中,getClass() 是一個(gè)非常重要的方法,它用于獲取對象的運(yùn)行時(shí)類信息,無論是調(diào)試代碼、反射操作,還是類型檢查,getClass() 都扮演著關(guān)鍵角色,本文將深入探討 getClass() 的使用方法、底層原理以及實(shí)際應(yīng)用場景,感興趣的朋友一起看看吧
    2024-12-12
  • 一文帶你了解Spring中存入Bean和獲取Bean的方式

    一文帶你了解Spring中存入Bean和獲取Bean的方式

    這篇文章主要帶大家了解Spring中存入Bean和獲取Bean的方式,文中的代碼示例講解的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-07-07
  • Mybatis-Plus環(huán)境配置與入門案例分析

    Mybatis-Plus環(huán)境配置與入門案例分析

    MyBatis-Plus 是一個(gè) Mybatis 增強(qiáng)版工具,在 MyBatis 上擴(kuò)充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在,本篇文章帶你配置環(huán)境并認(rèn)識它
    2022-03-03
  • Java踩坑記錄之BigDecimal類

    Java踩坑記錄之BigDecimal類

    這篇文章主要給大家介紹了關(guān)于Java踩坑記錄之BigDecimal類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 淺談MyBatis通用Mapper實(shí)現(xiàn)原理

    淺談MyBatis通用Mapper實(shí)現(xiàn)原理

    這篇文章主要介紹了淺談MyBatis通用Mapper實(shí)現(xiàn)原理,本文會(huì)先介紹通用 Mapper 的簡單原理,然后使用最簡單的代碼來實(shí)現(xiàn)這個(gè)過程。感興趣的小伙伴們可以參考一下
    2018-10-10
  • Java中關(guān)于Collections集合工具類的詳細(xì)介紹

    Java中關(guān)于Collections集合工具類的詳細(xì)介紹

    Java提供了一個(gè)操作Set、List和Map等集合的工具類:Collections,該工具提供了大量方法對集合元素進(jìn)行排序、查詢和修改等操作,還提供了將集合對象設(shè)置為不可變、對集合對象實(shí)現(xiàn)同步控制等方法
    2021-09-09
  • SpringBoot Bean花式注解方法示例下篇

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

    這篇文章主要介紹了SpringBoot Bean花式注解方法,很多時(shí)候我們需要根據(jù)不同的條件在容器中加載不同的Bean,或者根據(jù)不同的條件來選擇是否在容器中加載某個(gè)Bean
    2023-02-02
  • java分割字符串多種方法(附例子)

    java分割字符串多種方法(附例子)

    這篇文章主要給大家介紹了關(guān)于java分割字符串多種方法的相關(guān)資料,Java中有多種方法可以實(shí)現(xiàn)字符串分割,文中將每張方法都給出了代碼示例,需要的朋友可以參考下
    2023-10-10

最新評論