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

使用springboot在工具類中讀取配置文件(ClassPathResource)

 更新時(shí)間:2021年08月11日 10:59:46   作者:知識(shí)追求者  
這篇文章主要介紹了使用springboot在工具類中讀取配置文件(ClassPathResource),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot工具類中讀取配置文件

1、創(chuàng)建配置文件(application.properties)

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

2、創(chuàng)建工具類(PropertiesUtil.java)

package com.jeff.utils;
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
public class PropertiesUtil {
	private static String user;
	static {
		System.out.println("application.properties屬性文件讀取開始");
		ClassPathResource resource = new ClassPathResource("application.properties");
		try {
			Properties properties = PropertiesLoaderUtils.loadProperties(resource);
			user = properties.getProperty("spring.activemq.user");
			System.out.println("user的值:" + user);
		} catch (IOException e) {
			System.out.println("application.properties屬性文件讀取異常" + e);
		}
		System.out.println("application.properties屬性文件讀取完成");
	}
	public static String getUser() {
		System.out.println("獲取user的值:" + user);
		return user;
	}
}

3、創(chuàng)建測(cè)試類(MyController.java)

package com.jeff.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeff.utils.PropertiesUtil;
@RestController
public class MyController {
	@RequestMapping("myTest")
	public String myTest() {
		PropertiesUtil.getUser();
		return "success";
	}
}

4、打開瀏覽器訪問 http://localhost:8080/myTest,控制臺(tái)輸出結(jié)果

在這里插入圖片描述

在這里插入圖片描述

springboot讀取配置文件到靜態(tài)工具類

通常我們讀取配置文件可以用@Value注解和@Configuration,@ConfigurationProperties(prefix = "xxx")等注解,但是這種方式是無法把配置讀取到靜態(tài)變量的,如果我們想在項(xiàng)目初始化時(shí)把配置文件加載到一個(gè)工具類,然后通過靜態(tài)變量的方式調(diào)用的話我們就不能使用這兩種方法。

這時(shí)候,我們可以用Environment 來解決

不廢話了,直接上代碼

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
 * 
 * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
@Component
public class ConfigConstant {
  @Autowired
  private Environment env;
  
  public static String url;
  public static String param;
  
  @PostConstruct
  public void readConfig() {
    url = env.getProperty("config.url");
    param = env.getProperty("config.param");
  }
}

我寫完以后發(fā)現(xiàn)有些麻煩,下面是改進(jìn)的方法,不需要每個(gè)配置都去get一下,只需要把配置文件的key與工具類的靜態(tài)變量名寫成一樣的即可。

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
 * 
 * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置,變量名要與配置文件里寫的名一致
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
@Component
public class ConfigConstant {
  @Autowired
  private Environment env;
  
  public static String url;
  public static String name;
  
  
  @PostConstruct
  public void readConfig() throws Exception {
    String prefix = "config.";
    Field[] fields = ConfigConstant.class.getFields();
    for(Field field : fields ){
      field.set(null, getProperty(prefix + field.getName()));
    }
  }
  
  private String getProperty(String key) throws UnsupportedEncodingException {
    return new String(env.getProperty(key).getBytes("ISO-8859-1"), "UTF-8");
  }
}

大哥說這樣寫依賴spring, 單測(cè)調(diào)代碼的時(shí)候不方便,所以又寫了一個(gè)不依賴spring的版本。

import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Properties;
/**
 * 
 * @Description: 配置常量類——根據(jù)不同的spring-profile加載不同的配置
 *               變量名把配置文件的key中的"."替換成"_"命名
 * @author: eric.zhang
 * @date: 2018年7月20日 上午10:59:24
 */
public class ConfigConstant {
  public static String CONFIG_URL;
  public static String CONFIG_NAME;
  static {
    try {
      Properties props = new Properties();
      props.load(new InputStreamReader(
          ConfigConstant.class.getClassLoader().getResourceAsStream("application.properties"),
          "UTF-8"));
      String profile = props.getProperty("spring.profiles.active");
      String envFile = "application-" + profile + ".properties";
      Properties envProps = new Properties();
      envProps.load(new InputStreamReader(
          ConfigConstant.class.getClassLoader().getResourceAsStream(envFile), "UTF-8"));
      Field[] fields = ConfigConstant.class.getFields();
      for (Field field : fields) {
        field.set(null, envProps.getProperty(field.getName().replace("_", ".").toLowerCase()));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

相關(guān)文章

  • 詳解Spring獲取配置的三種方式

    詳解Spring獲取配置的三種方式

    這篇文章主要為大家詳細(xì)介紹了Spring獲取配置的三種方式:@Value方式動(dòng)態(tài)獲取單個(gè)配置、@ConfigurationProperties+前綴方式批量獲取配置以及Environment動(dòng)態(tài)獲取單個(gè)配置,感興趣的可以了解一下
    2022-03-03
  • Java線程同步方法實(shí)例總結(jié)

    Java線程同步方法實(shí)例總結(jié)

    這篇文章主要介紹了Java線程同步方法,結(jié)合實(shí)例形式總結(jié)分析了Java線程同步、并發(fā)控制相關(guān)實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • Java通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序

    Java通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序

    用Java編寫應(yīng)用時(shí),有時(shí)需要在程序中調(diào)用另一個(gè)現(xiàn)成的可執(zhí)行程序或系統(tǒng)命令,這篇文章主要給大家介紹了關(guān)于Java如何通過Process類Runtime.getRuntime().exec()執(zhí)行bat腳本程序的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的方法實(shí)例

    Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡(jiǎn)易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件

    基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件

    這篇文章主要為大家詳細(xì)介紹了基于Retrofit+Rxjava實(shí)現(xiàn)帶進(jìn)度顯示的下載文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 通過代碼實(shí)例深入解析Java重寫和重載

    通過代碼實(shí)例深入解析Java重寫和重載

    這篇文章主要介紹了通過代碼實(shí)例深入解析Java重寫和重載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • SpringMVC中的HandlerAdapter解析

    SpringMVC中的HandlerAdapter解析

    這篇文章主要介紹了SpringMVC中的HandlerAdapter解析,HandlerAdapter是一個(gè)關(guān)鍵的組件,用于將請(qǐng)求與處理程序方法進(jìn)行適配和調(diào)度,它充當(dāng)了控制器和處理程序之間的橋梁,負(fù)責(zé)將請(qǐng)求的參數(shù)和處理程序方法進(jìn)行匹配,并將結(jié)果返回給前端,需要的朋友可以參考下
    2023-10-10
  • Java排序的那些事之sort方法的使用詳解

    Java排序的那些事之sort方法的使用詳解

    sort方法用于對(duì)數(shù)組的元素進(jìn)行排序。排序順序可以是字母或數(shù)字,并按升序或降序。默認(rèn)排序順序?yàn)榘醋帜干?,?dāng)數(shù)字是按字母順序排列時(shí)"40"將排在"5"前面。使用數(shù)字排序,你必須通過一個(gè)函數(shù)作為參數(shù)來調(diào)用。這些說起來可能很難理解,你可以通過本篇文章進(jìn)一步了解它
    2021-09-09
  • Springboot整合hutool驗(yàn)證碼的實(shí)例代碼

    Springboot整合hutool驗(yàn)證碼的實(shí)例代碼

    在 Spring Boot 中,你可以將 Hutool 生成驗(yàn)證碼的功能集成到 RESTful API 接口中,這篇文章主要介紹了Springboot整合hutool驗(yàn)證碼,需要的朋友可以參考下
    2024-08-08
  • idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文)

    idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文)

    這篇文章主要介紹了idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01

最新評(píng)論