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

springBoot如何動態(tài)加載資源文件

 更新時間:2021年12月15日 10:25:50   作者:wangtuoyuan  
這篇文章主要介紹了springBoot如何動態(tài)加載資源文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springBoot動態(tài)加載資源文件

在實際項目中資源信息如果能夠動態(tài)獲取在修改線上產(chǎn)品配置時極其方便,下面來展示一個加載動態(tài)獲取資源的案例,而不是加載寫死的properties文件信息。

首先構(gòu)造PropertySource,然后將其添加到Enviroment中。

構(gòu)造DynamicLoadPropertySource

package com.wangh.test;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.MapPropertySource;
/**
 * 動態(tài)加載配置文件
 * @ClassName:DynamicLoadPropertySource
 * @author:Wh
 * @date: 2017年9月14日 下午3:22:54
 */
public class DynamicLoadPropertySource extends MapPropertySource{
    private static Logger log = LoggerFactory.getLogger(DynamicLoadPropertySource.class);
    private static Map<String, Object> map = new ConcurrentHashMap<String, Object>(64); 
    private static ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
    static {  
        scheduled.scheduleAtFixedRate(new Runnable() {  
            @Override  
            public void run() {  
                map = dynamicLoadMapInfo();  
            }  
        }, 1, 10, TimeUnit.SECONDS);  
    }  
    /**
     * @param name
     * @param source
     */
    public DynamicLoadPropertySource(String name, Map<String, Object> source) {
        super(name, map);
    }
    @Override  
    public Object getProperty(String name) {  
        return map.get(name);  
    }  
    /**
     * //動態(tài)獲取資源信息  
     * @return  
     */
    protected static Map<String, Object> dynamicLoadMapInfo() {
        return mockMapInfo(); 
    }
    /**
     * @return  
     */
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    private static Map<String, Object> mockMapInfo() {
        Map<String, Object> map = new HashMap<String, Object>(); 
        map = getProperties();
        log.info("data{};currentTime:{}", map.get("spring.datasource.url"), sdf.format(new Date())); 
        return map;
    }
    /**
     * 獲取配置文件信息
     * @return
     */
    public static Map<String, Object> getProperties() {
        Properties props = new Properties();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            InputStream in = ClassLoader.getSystemResourceAsStream("pro.properties");
            props.load(in);
            Enumeration<?> en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = props.getProperty(key);
                map.put(key, property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}

添加到Enviroment

package com.wangh.test;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;
/**
 * 加載動態(tài)配置信息  
 * @ClassName:DynamicConfig
 * @author:Wh
 * @date: 2017年9月14日 下午3:38:17
 */
@Configuration
public class DynamicConfig {
    //資源信息元數(shù)據(jù):PropertySource包含name和泛型,一份資源信息存在唯一的name以及對應(yīng)泛型數(shù)據(jù),在這里設(shè)計為泛型表明可拓展自定.PropertySource在集合中的唯一性只能去看name
    public static final String DYNAMIC_CONFIG_NAME = "dynamic_config"; 
    @Autowired  
    AbstractEnvironment environment;
    @PostConstruct  
    public void init() {  
        environment.getPropertySources().addFirst(new DynamicLoadPropertySource(DYNAMIC_CONFIG_NAME, null));  
    }  
}

springBoot靜態(tài)資源動態(tài)加載

  • spring.resources.static-locations 表示讀取靜態(tài)資源的位置
  • spring.mvc.static-path-pattern 表示讀取靜態(tài)資源路徑

舉例說明

spring.resources.static-locations=file:${user.home}/report
spring.mvc.static-path-pattern=/resources/**
  • spring.http.encoding.force=true 添加該配置解決中文亂碼問題
  • http://localhost:8080/resources/1.html

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

相關(guān)文章

  • Springboot中集成Swagger2框架的方法

    Springboot中集成Swagger2框架的方法

    這篇文章主要介紹了Springboot中集成Swagger2框架的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-12-12
  • SpringBoot打印POST請求原始入?yún)ody體方式

    SpringBoot打印POST請求原始入?yún)ody體方式

    這篇文章主要介紹了SpringBoot打印POST請求原始入?yún)ody體方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java查找圖中兩點之間所有路徑

    java查找圖中兩點之間所有路徑

    這篇文章主要為大家詳細介紹了java查找圖中兩點之間所有路徑,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • java高并發(fā)下解決AtomicLong性能瓶頸方案LongAdder

    java高并發(fā)下解決AtomicLong性能瓶頸方案LongAdder

    這篇文章主要為大家介紹了java高并發(fā)下解決AtomicLong性能瓶頸方案LongAdder,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • JEE與Spring Boot代碼性能比較分析

    JEE與Spring Boot代碼性能比較分析

    JavaEE與Spring Boot其實很難比較測試,前者適合單體SOA架構(gòu),后者適合微服務(wù),但是還是有好事者把兩者放在一起比較性能。這篇文章主要介紹了JEE與Spring Boot代碼性能比較,需要的朋友可以參考下
    2018-11-11
  • maven+springboot打成jar包的方法

    maven+springboot打成jar包的方法

    這篇文章主要介紹了maven+springboot打成jar包的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • Java8 Lambda表達式模板方法實現(xiàn)解析

    Java8 Lambda表達式模板方法實現(xiàn)解析

    這篇文章主要介紹了Java8 Lambda表達式模板方法實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • java多線程之Balking模式介紹

    java多線程之Balking模式介紹

    大家好,本篇文章主要講的是java多線程之Balking模式介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • java如何創(chuàng)建普通二叉樹

    java如何創(chuàng)建普通二叉樹

    這篇文章主要介紹了java如何創(chuàng)建普通二叉樹的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java深入講解異常處理try?catch的使用

    Java深入講解異常處理try?catch的使用

    這篇文章主要介紹了Java異常處理機制try?catch流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-06-06

最新評論