背景

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



可以根据不同分类的文件来管理配置,然后统一在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的更多相关文章

  1. spring项目读取配置文件

    Spring项目在运用中读取配置文件有两种方式: 通过项目的配置文件读取 在spring-context.xml里面加入以下代码 在运用到的类里面加入 @Value("#{configPro ...

  2. Python项目读取配置的几种方式

    1. 将配置写在Python文件中 配置文件(config.py 或 settings.py) 通常放置在程序源代码的目录,方便引用 配置文件 # settings.py class Config(o ...

  3. Spring项目读取resource下的文件

    目录 一.前提条件 二.使用ClassPathResource类读取 2.1.Controller.service中使用ClassPathResource 2.2.单元测试使用ClassPathRes ...

  4. Spring Boot读取配置的几种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USAinfo.company=Springinfo.degree=hi ...

  5. Spring Boot读取配置的 5 种方式

    读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company=Spring info.degree= ...

  6. web项目中配置多个数据源

    web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源,  ...

  7. 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)

    非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...

  8. Spring Boot项目属性配置

    接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将se ...

  9. 使用java配置来构建spring项目

    java配置是Spring4.x推荐的配置方式,可以完全代替xml配置,java配置是通过@Configuration和@Bean来实现的.@Configuration声明当前类是一个配置类,相当于S ...

随机推荐

  1. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  2. jqgrid子表格

    .前台 <%-- builed by manage.aspx.cmt [ver:] at // :: --%> <%@ Page Language="C#" Au ...

  3. WPF禁止拖拽窗口到边缘自动最大化

    近期有个需求,可以通过拖拽改变窗口大小,但是不允许窗口最大化.最小化.拖到边缘的时候也不能自动最大化. 要想禁止拖拽窗口到边缘自动最大化,只要改注册表即可,但是系统所有应用都会被禁止. 1.运行reg ...

  4. 别人的Linux私房菜(1)计算机概论

    计算机主板 早期两个网桥控制通信,北桥连接速度比较快的CPU.内存.显卡.南桥连接较慢的接口,如硬盘,USB,网卡等.北桥的控制器集成到了CPU中. CPU工作频率 外频:CPU与外部组件进行数据传输 ...

  5. GameObject.SendMessage

    Message相关有3条指令:    要接收消息的GameObject.SendMessage ("函数名",参数,SendMessageOptions)  //自身和父Objec ...

  6. Idea中如何将web项目打包成war包并放到tomcat中启动

    第一步:在idea中选中Artifacts.右侧勾选Build on make生成war包,如下图 第二步:将target文件夹里面的war包拷贝到tomcat文件下的webapp目录下 第三步:修改 ...

  7. centos下利用yum安装LAMP(Linux+Apache+MySQL+PHP)及配置

    先说下我的实践配置,centos6.5(64位),联网 安装前准备:关闭防火墙 service iptables stop 安装MySQL 打开终端,root用户 1 yum install mysq ...

  8. springboot深入学习(二)-----profile配置、运行原理、web开发

    一.profile配置 通常企业级应用都会区分开发环境.测试环境以及生产环境等等.spring提供了全局profile配置的方式,使得在不同环境下使用不同的applicaiton.properties ...

  9. linux系统配置参数修改

    一.永久修改主机名修改/etc/sysconfig/network,在里面指定主机名称HOSTNAME=然后执行命令hostname 主机名这个时候可以注销一下系统,再重登录之后就行了. 或者修改/e ...

  10. BZOJ 3259 [Sdoi2014]数表 (莫比乌斯反演 + 树状数组)

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2321  Solved: 1187[Submit][Status ...