springBoot如何动态加载资源文件

网友投稿 854 2022-11-15

springBoot如何动态加载资源文件

springBoot如何动态加载资源文件

目录springBoot动态加载资源文件构造DynamicLoadPropertySource添加到EnviromentspringBoot静态资源动态加载举例说明

springBoot动态加载资源文件

在实际项目中资源信息如果能够动态获取在修改线上产品配置时极其方便,下面来展示一个加载动态获取资源的案例,而不是加载写死的properties文件信息。

首先构造PropertySource,然后将其添加到Enviroment中。

构造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;

/**

* 动态加载配置文件

* @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 map = new ConcurrentHashMap(64);

private static ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);

static {

scheduled.scheduleAtFixedRate(new Runnable() {

@Override

public void run() {

map = dynamicLoadMapInfo();

bbJuL }

}, 1, 10, TimeUnit.SECONDS);

}

/**

* @param name

* @param source

*/

public DynamicLoadPropertySource(String name, Map source) {

super(name, map);

}

@Override

public Object getProperty(String name) {

return map.get(name);

}

/**

* //动态获取资源信息

* @return

bbJuL */

protected static Map dynamicLoadMapInfo() {

return mockMapInfo();

}

/**

* @return

*/

private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

private static Map mockMapInfo() {

Map map = new HashMap();

map = getProperties();

log.info("data{};currentTime:{}", map.get("spring.datasource.url"), sdf.format(new Date()));

return map;

}

/**

* 获取配置文件信息

* @return

*/

public static Map getProperties() {

Properties props = new Properties();

Map map = new HashMap();

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;

/**

* 加载动态配置信息

* @ClassName:DynamicConfig

* @author:Wh

* @date: 2017年9月14日 下午3:38:17

*/

@Configuration

public class DynamicConfig {

//资源信息元数据:PropertySource包含name和泛型,一份资源信息存在唯一的name以及对应泛型数据,在这里设计为泛型表明可拓展自定.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));

}

}

springBobbJuLot静态资源动态加载

spring.resources.static-locations 表示读取静态资源的位置

sprinbbJuLg.mvc.static-path-pattern 表示读取静态资源路径

举例说明

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Nginx 面试题 40 问
下一篇:[MAC] MAC上的ssh工具Shuttle 安装与配置简介
相关文章

 发表评论

暂时没有评论,来抢沙发吧~