spring项目读取配置的demo
背景
读取配置是基础能力,研发这个模式不错,可以从不同配置中读取数据,如下图:

可以根据不同分类的文件来管理配置,然后统一在conf中配置哪些文件
package com.jwen.platform.config;
import com.jwen.platform.exception.AppConfigurationException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public enum Configurations {
INSTANCE;
private final static String KEY_VALUE_SEPARATOR = "=";
private final static String CONFIGURATION_FILE = "application.conf";
private final static String OTHER_CONFIGURATION_FILE_KEY = "app_include_file";
private List<String> otherConfigurationFiles = new ArrayList<>();
private Map<String, String> configurations = new HashMap<>();
Configurations() {
try {
init();
} catch (IOException | URISyntaxException e) {
throw new AppConfigurationException("has error in your application.conf", e);
}
}
private void init() throws IOException, URISyntaxException {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
String s = "";
List<String> lines = new ArrayList<String>();
while ((s = br.readLine()) != null) {
lines.add(s);
}
// 关闭流
resourceAsStream.close();
br.close();
initConfigurations(lines);
initOtherConfigurations();
}
private void initOtherConfigurations() throws URISyntaxException, IOException {
for (String fileName : otherConfigurationFiles) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
String s = "";
List<String> lines = new ArrayList<String>();
while ((s = br.readLine()) != null) {
lines.add(s);
}
// 关闭流
resourceAsStream.close();
br.close();
initConfigurations(lines);
}
otherConfigurationFiles = null;
}
private boolean isOtherConfigurationKey(String key) {
return OTHER_CONFIGURATION_FILE_KEY.equalsIgnoreCase(key);
}
private void initConfigurations(List<String> lines) throws URISyntaxException, IOException {
lines.stream().forEach(line -> {
int keyValueSeparatorIndex = line.indexOf(KEY_VALUE_SEPARATOR);
if (isLegalConfigurationLine(line)) {
String key = line.substring(0, keyValueSeparatorIndex).trim();
String value = line.substring(keyValueSeparatorIndex + 1, line.length()).trim();
if (isOtherConfigurationKey(key)) {
otherConfigurationFiles.add(value);
} else {
configurations.put(key, value);
}
}
});
}
private boolean isLegalConfigurationLine(String line) {
Pattern pattern = Pattern.compile("^[a-zA-Z](.*?)=(.*?)");
return pattern.matcher(line).matches();
}
public String get(String key) {
return configurations.get(key);
}
public static void main(String[] args) {
System.out.println(Configurations.INSTANCE.get("test.on"));
}
}
spring项目读取配置的demo的更多相关文章
- spring项目读取配置文件
Spring项目在运用中读取配置文件有两种方式: 通过项目的配置文件读取 在spring-context.xml里面加入以下代码 在运用到的类里面加入 @Value("#{configPro ...
- Python项目读取配置的几种方式
1. 将配置写在Python文件中 配置文件(config.py 或 settings.py) 通常放置在程序源代码的目录,方便引用 配置文件 # settings.py class Config(o ...
- Spring项目读取resource下的文件
目录 一.前提条件 二.使用ClassPathResource类读取 2.1.Controller.service中使用ClassPathResource 2.2.单元测试使用ClassPathRes ...
- Spring Boot读取配置的几种方式
读取application文件 在application.yml或者properties文件中添加: info.address=USAinfo.company=Springinfo.degree=hi ...
- Spring Boot读取配置的 5 种方式
读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company=Spring info.degree= ...
- web项目中配置多个数据源
web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源, ...
- 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)
非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...
- Spring Boot项目属性配置
接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将se ...
- 使用java配置来构建spring项目
java配置是Spring4.x推荐的配置方式,可以完全代替xml配置,java配置是通过@Configuration和@Bean来实现的.@Configuration声明当前类是一个配置类,相当于S ...
随机推荐
- LFI/RFI总结
目录 0×01 文件包含简介 服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行,这会为开发者节省大量的时间.这意味着您可以创建供所有网页引用的标准页眉或菜单 ...
- 2018年设计师都在用的PS切图插件--摹客iDoc
终于找到你,我梦寐以求的PS切图插件.曾几何时,设计师在完成设计稿之后高效的输出标注切图一直是设计师的噩梦.为什么这么说呢?开发要的那么多尺寸,我到底该怎么切图?iPhone的版本已经不少了,更别提安 ...
- Spring 常见注解
@Component:标准一个普通的spring Bean类. @Controller:标注一个控制器组件类. @Service:标注一个业务逻辑组件类. @Repository:标注一个DAO组件类 ...
- 编译sgbm_ros中遇到的问题
出现的问题 这个会报错 1.解决方法是在文件sudo gedit /usr/local/cuda/include/crt/common_functions.h中注释掉如下 #define __CUDA ...
- Mercurial和Git的主要区别(zz)
Mercurial和Git的主要区别 17 August 2008 1.Mercurial用Python开发,Git用C开发,相对来说,Git比较快,但是Mercurial的性能也不差 2.Mercu ...
- unity延时函数
新建一个工具类 public class DelayToInvoke : MonoBehaviour{ public static IEnumerator DelayToInvokeDo(Action ...
- WM_PAINT和WM_ERASEBKGND消息
1.OnPaint()函数是窗口重绘消息WM_PAINT的响应函数,当窗口重绘时会产生WM_ERASEBKGND消息和WM_PAINT消息,而且WM_ERASEBKGND会先于WM_PAINT产生,所 ...
- 微信小程序的新的
app.request.get('http://ele.kassing.cn/v1/pois',this.data.city).then(res=>{ console.log(res) this ...
- new命令简化的内部流程
构造函数返回对象的一些问题: function fn(name,age){ this.name = name; this.age = age; //return 23; 忽略数字,直接返回原有对象 / ...
- 查阅JDK,collection与collections区别大
看起来collection,和collections相像,但其中的差别之大你造吗? Collection是Collection层次结构中的根接口.Collection表示一组对象,这也对象也称为col ...