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

Spring基于注解讀取外部配置文件

 更新時(shí)間:2020年12月04日 10:33:13   作者:codedot  
這篇文章主要介紹了Spring基于注解讀取外部配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、使用注解@PropertySource

指定路徑

使用 @PropertySource 指定配置文件路徑,支持 properties 和 XML 的配置文件,但不支持 yml。

屬性賦值

可以用注解 @Value 對(duì)屬性直接賦值、${}獲取配置文件的值、SPEL表達(dá)式#{}。

  • 直接賦值:@Value("name jack")
  • 讀取配置文件:@Value("${user.age}")
  • 指定默認(rèn)值:@Value("${user.desc:default desc}") 表示如果沒有user.desc的配置,則賦值為default desc
  • SPEL表達(dá)式:@Value("#{'${user.username}'?.toUpperCase()}") 表示將從配置文件讀取的值轉(zhuǎn)為大寫,?可以不填,表示如果沒有user.username的配置,則忽略

例子

config.properties內(nèi)容

ps.datasource.driverClassName=com.mysql.jdbc.Driver
ps.datasource.jdbcUrl=jdbc:mysql://localhost:3306/spring?useTimezone=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&tcpRcvBuf=1024000&useOldAliasMetadataBehavior=true&useSSL=false&rewriteBatchedStatements=true&useAffectedRows=true
ps.datasource.username=root
ps.datasource.password=root
ps.datasource.minIdle=1
ps.datasource.maxPoolSize=10
ps.datasource.connectionTimeout=3000
ps.datasource.idleTimeout=300000

配置類

/**
 * 使用@PropertySource指定具體的配置文件,用@Value設(shè)置具體的屬性值, 不支持yml
 */
@Component
@PropertySource("classpath:config.properties")
public class DbProperties {

  @Value("${ps.datasource.driverClassName}")
  private String driverClassName;
  @Value("${ps.datasource.jdbcUrl}")
  private String jdbcUrl;
  @Value("${ps.datasource.username}")
  private String username;
  @Value("${ps.datasource.password}")
  private String password;
  @Value("${ps.datasource.minIdle}")
  private int minIdle;
  @Value("${ps.datasource.maxPoolSize}")
  private int maxPoolSize;
  @Value("${ps.datasource.connectionTimeout}")
  private int connectionTimeout;
  @Value("${ps.datasource.idleTimeout}")
  private int idleTimeout;

  public String getDriverClassName() {
    return driverClassName;
  }

  public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
  }

  public String getJdbcUrl() {
    return jdbcUrl;
  }

  public void setJdbcUrl(String jdbcUrl) {
    this.jdbcUrl = jdbcUrl;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public int getMinIdle() {
    return minIdle;
  }

  public void setMinIdle(int minIdle) {
    this.minIdle = minIdle;
  }

  public int getMaxPoolSize() {
    return maxPoolSize;
  }

  public void setMaxPoolSize(int maxPoolSize) {
    this.maxPoolSize = maxPoolSize;
  }

  public int getConnectionTimeout() {
    return connectionTimeout;
  }

  public void setConnectionTimeout(int connectionTimeout) {
    this.connectionTimeout = connectionTimeout;
  }

  public int getIdleTimeout() {
    return idleTimeout;
  }

  public void setIdleTimeout(int idleTimeout) {
    this.idleTimeout = idleTimeout;
  }

  @Override
  public String toString() {
    return "DbProperties{" +
        "driverClassName='" + driverClassName + '\'' +
        ", jdbcUrl='" + jdbcUrl + '\'' +
        ", username='" + username + '\'' +
        ", password='" + password + '\'' +
        ", minIdle=" + minIdle +
        ", maxPoolSize=" + maxPoolSize +
        ", connectionTimeout=" + connectionTimeout +
        ", idleTimeout=" + idleTimeout +
        '}';
  }
}

二、使用Environment

/**
 * Environment可以獲取classpath下配置的屬性值,無需指定具體的配置文件。 不支持yml
 */
@Component
public class UserProperties {

  @Autowired
  private Environment env;

  public String getUserName() {
    return env.getProperty("user.name");
  }

  public String getPassword() {
    return env.getProperty("user.password");
  }
}

三、使用PropertiesLoaderUtils

try {
      Properties properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
      System.out.println(properties.getProperty("user.name"));
    } catch (IOException e) {
      e.printStackTrace();
    }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 4位吸血鬼數(shù)字的java實(shí)現(xiàn)思路與實(shí)例講解

    4位吸血鬼數(shù)字的java實(shí)現(xiàn)思路與實(shí)例講解

    今天小編就為大家分享一篇關(guān)于4位吸血鬼數(shù)字的java實(shí)現(xiàn)思路與實(shí)例講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • JavaFX 監(jiān)聽窗口關(guān)閉事件實(shí)例詳解

    JavaFX 監(jiān)聽窗口關(guān)閉事件實(shí)例詳解

    這篇文章主要介紹了JavaFX 監(jiān)聽窗口關(guān)閉事件實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼

    Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼

    這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • MyBatis動(dòng)態(tài)創(chuàng)建表的實(shí)例代碼

    MyBatis動(dòng)態(tài)創(chuàng)建表的實(shí)例代碼

    在項(xiàng)目需求中,我們經(jīng)常會(huì)遇到動(dòng)態(tài)操作數(shù)據(jù)表的需求,常見的我們會(huì)把日志、設(shè)備實(shí)時(shí)位置信息等存入數(shù)據(jù)表,并且以一定時(shí)間段生成一個(gè)表來存儲(chǔ)。接下來通過本文給大家介紹MyBatis動(dòng)態(tài)創(chuàng)建表的方法,感興趣的朋友一起看看吧
    2018-07-07
  • Java多線程并發(fā)執(zhí)行demo代碼實(shí)例

    Java多線程并發(fā)執(zhí)行demo代碼實(shí)例

    這篇文章主要介紹了Java多線程并發(fā)執(zhí)行demo代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java 動(dòng)態(tài)代理原理分析

    Java 動(dòng)態(tài)代理原理分析

    這篇文章主要介紹了Java 動(dòng)態(tài)代理 原理分析的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握動(dòng)態(tài)代理的原理,需要的朋友可以參考下
    2017-10-10
  • Spark SerializedLambda錯(cuò)誤的兩種解決方案

    Spark SerializedLambda錯(cuò)誤的兩種解決方案

    這篇文章主要介紹了Spark SerializedLambda錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    這篇文章主要介紹了java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • java的各種集合為什么不安全(List、Set、Map)以及代替方案

    java的各種集合為什么不安全(List、Set、Map)以及代替方案

    這篇文章主要介紹了java的各種集合為什么不安全(List、Set、Map)以及代替方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用

    Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用

    這篇文章主要介紹了Java基礎(chǔ)之Bean的創(chuàng)建、定位和使用,文中有非常詳細(xì)的圖文示例及代碼,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

通许县| 枝江市| 海城市| 邳州市| 贡嘎县| 宁阳县| 鄯善县| 海盐县| 贵港市| 社会| 会昌县| 连山| 望奎县| 沈丘县| 武威市| 双城市| 莒南县| 海南省| 板桥市| 二连浩特市| 武威市| 松原市| 海宁市| 广水市| 阳东县| 萨迦县| 九龙坡区| 五常市| 佳木斯市| 嘉兴市| 方山县| 军事| 紫阳县| 伽师县| 扎囊县| 上林县| 蕲春县| 大丰市| 汪清县| 福海县| 保康县|