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;
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);
}
public DynamicLoadPropertySource(String name, Map<String, Object> source) {
super(name, map);
}
@Override
public Object getProperty(String name) {
return map.get(name);
}
protected static Map<String, Object> dynamicLoadMapInfo() {
return mockMapInfo();
}
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;
}
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;
@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));
}
}
springBoot静态资源动态加载
spring.resources.static-locations
表示读取静态资源的位置spring.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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。