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

關于@PropertySource配置的用法解析

 更新時間:2022年03月25日 11:16:41   作者:馬爾斯的藍色  
這篇文章主要介紹了關于@PropertySource配置的用法解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@PropertySource配置用法

功能

加載指定的屬性文件(*.properties)到 Spring 的 Environment 中??梢耘浜?@Value 和@ConfigurationProperties 使用。

@PropertySource 和 @Value組合使用,可以將自定義屬性文件中的屬性變量值注入到當前類的使用@Value注解的成員變量中。

@PropertySource 和 @ConfigurationProperties組合使用,可以將屬性文件與一個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();
    /**
     * 如果指定的屬性源不存在,是否要忽略這個錯誤
     */
    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());
? ? }
}

啟動項目即可看到效果。 

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

@PropertySource注解

@PropertySource是Spring boot為了方便引入properties配置文件提供的一個注解,可以標注在SpringBoot的啟動類上,還可以標注在配置類(使用@Configuration標注的類)上。

例如

@PropertySource(value = {"classpath:box.properties"})

將classpath下的box.properties,注入到Spring環(huán)境中,使用@Value("${key}")取值。

示例

box.properties文件:

# 工具箱配置?
preserveFilePath=/box/webserver/uploadfile/preservefile/

注入:

@SpringBootApplication
@PropertySource(value = {"classpath:box.properties"})
public class ToolboxApiApplication {
?
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(ToolboxApiApplication.class, args);
? ? }?
}

取值: 

@RestController
public class DeleteFileController {
? ? @Value("${preserveFilePath}")
? ? private String preserveFilePath;
?
? ? @GetMapping("/deleteFile")
? ? public void test(){
? ? ? ? System.out.println(preserveFilePath);
? ? }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

  • SpringCloud feign服務熔斷下的異常處理操作

    SpringCloud feign服務熔斷下的異常處理操作

    這篇文章主要介紹了SpringCloud feign服務熔斷下的異常處理操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法

    FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法

    本文主要介紹了FluentMybatis實現(xiàn)mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中的反射機制基本運用詳解

    Java中的反射機制基本運用詳解

    這篇文章主要介紹了Java 反射機制原理與用法,結合實例形式詳細分析了Java反射機制的相關概念、原理、基本使用方法及操作注意事項,需要的朋友可以參考下
    2021-08-08
  • Spring MVC 攔截器實現(xiàn)代碼

    Spring MVC 攔截器實現(xiàn)代碼

    本篇文章主要介紹了Spring MVC 攔截器的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析

    Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析

    HQL是Hibernate框架中提供的關系型數(shù)據(jù)庫操作腳本,當然我們也可以使用原生的SQL語句,這里我們來看一下在Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析:
    2016-06-06
  • Spring Data JPA 建立表的聯(lián)合主鍵

    Spring Data JPA 建立表的聯(lián)合主鍵

    這篇文章主要介紹了Spring Data JPA 建立表的聯(lián)合主鍵。本文詳細的介紹了2種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 淺談Java 中的單元測試

    淺談Java 中的單元測試

    這篇文章主要介紹了Java 中的單元測試的相關資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • Java后臺生成圖片的完整步驟

    Java后臺生成圖片的完整步驟

    在一些詳情頁面中,可能需要對上傳到服務器中的圖片生成以縮略圖的形式展示,這篇文章主要給大家介紹了關于Java后臺生成圖片的相關資料,需要的朋友可以參考下
    2021-08-08
  • mybatis-plus分頁無效問題解決

    mybatis-plus分頁無效問題解決

    本文主要介紹了mybatis-plus分頁無效問題解決,原因是配置分頁插件的版本問題,舊版本和新版本的MyBatis-Plus需要不同的分頁配置,感興趣的可以了解一下
    2025-03-03
  • Java停止線程的3種方法

    Java停止線程的3種方法

    這篇文章主要分享Java停止線程的3種方法,分別是自定義中斷標識符,停止線程、使用線程中斷方法interrupt停止線程、使用stop停止線程。下文詳細介紹需要的小伙伴可以參考一下
    2022-05-05

最新評論

察哈| 嘉兴市| 凤凰县| 西平县| 治多县| 共和县| 安阳市| 博爱县| 怀集县| 永寿县| 山东省| 三河市| 阿荣旗| 呼伦贝尔市| 河源市| 西吉县| 察哈| 凉城县| 集安市| 五原县| 乐清市| 长垣县| 虹口区| 泽州县| 轮台县| 施甸县| 个旧市| 庄浪县| 措勤县| 南召县| 化州市| 灵石县| 咸宁市| 沙田区| 宁化县| 娱乐| 湖北省| 漾濞| 库车县| 临邑县| 保康县|