在大多数项目中我们经常会使用到读取配置文件,用于适配自定义的属性值等,本教程我们主要通过实现对Properties的解析实现基于Guice的配置解析Module.

基础环境


技术 版本
Java 1.8+
Guice 4.2.3

初始化项目


  • 初始化项目
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-integration-configuration -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
  • 修改pom.xml增加Guice依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>lc-guice</artifactId><groupId>io.edurt.lc.guice</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>guice-integration-configuration</artifactId><name>Learning Center for Guice Integration(Configuration)</name><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>com.google.inject</groupId><artifactId>guice</artifactId><version>4.2.3</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies></project>

guice: guice就是我们核心要使用的依赖

构建PropertiesUtils


PropertiesUtils主要用于我们对Properties类型文件的解析.

  • src/main/java目录下新建 io.edurt.lc.guice.PropertiesUtils 类文件,在文件输入以下内容
package io.edurt.lc.guice;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils
{private PropertiesUtils(){}public static Properties loadProperties(String... resourcesPaths){Properties props = new Properties();for (String location : resourcesPaths) {File propertiesFile = new File(location);try (InputStream inputStream = new FileInputStream(propertiesFile)) {props.load(inputStream);}catch (IOException ex) {ex.printStackTrace();}}return props;}public static Integer getIntValue(Properties properties, String key, Integer defaultValue){return Integer.valueOf(getStringValue(properties, key, String.valueOf(defaultValue)));}public static String getStringValue(Properties properties, String key, String defaultValue){if (properties == null) {return defaultValue;}if (!properties.containsKey(key)) {return defaultValue;}return String.valueOf(properties.getOrDefault(key, defaultValue));}public static Boolean getBoolValue(Properties properties, String key, Boolean defaultValue){return Boolean.valueOf(getStringValue(properties, key, String.valueOf(defaultValue)));}
}
我们在工具类中提供了几个便捷的方法: - `getIntValue` 获取Integer类型数据   - `getStringValue` 获取字符串类型数据- `getBoolValue` 获取Boolean类型数据
  • src/main/java目录下新建 io.edurt.lc.guice.ConfigurationModule 类文件,在文件输入以下内容
package io.edurt.lc.guice;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.Properties;
public class ConfigurationModuleextends AbstractModule
{private Properties bootstrapConfiguration;public ConfigurationModule(Properties bootstrapConfiguration){this.bootstrapConfiguration = bootstrapConfiguration;}public ConfigurationModule(String configurationFilePath){if (configurationFilePath != null) {this.bootstrapConfiguration = PropertiesUtils.loadProperties(configurationFilePath);}}@Overrideprotected void configure(){Names.bindProperties(binder(), bootstrapConfiguration);}
}

测试示例


  • 接下来在src/test/java目录创建 io.edurt.lc.guice.TestConfigurationModule 类文件进行定义的服务进行测试,添加以下代码
package io.edurt.lc.guice;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestConfigurationModule
{private String path;private Injector injector;@Beforepublic void before(){path = this.getClass().getResource("/file.properties").getPath();injector = Guice.createInjector(new ConfigurationModule(path));}@Afterpublic void after(){}/*** Method: configure()*/@Testpublic void testConfigure(){Assert.assertTrue(injector.getBindings().toString().contains("Test1"));}
}
  • test/resources目录下新建 file.properties 文件,内容如下:
name=Test1

我们运行程序输出

Connected to the target VM, address: '127.0.0.1:63962', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:63962', transport: 'socket'

由于我们使用了断言来操作,所以正常运行后系统不会出现任何输入,如果有错误信息,那么控制台会抛出错误信息.

源码地址


GitHub

Guice集成Properties配置相关推荐

  1. s2sh集成dataSource配置无效的问题 -Access denied for user 'sa'@'localhost'

    s2sh集成dataSource配置无效的问题 -Access denied for user 'sa'@'localhost' 博客分类: j2ee s2sh web架构 strutshiberna ...

  2. Jenkins——Jenkins介绍+基于云平台的Jenkins安装和持续集成环境配置(插件+用户权限+凭据+Maven打包)

    Jenkins--Jenkins介绍+基于云平台的Jenkins安装和持续集成环境配置(插件+用户权限+凭据+Maven打包) 持续集成及Jenkins介绍 软件开发生命周期 什么是持续集成? 持续集 ...

  3. Jenkins安装和持续集成环境配置

    Jenkins安装和持续集成环境配置 持续集成说明 Jenkins安装 Jenkins插件管理 下载中文汉化插件 Jenkins用户权限管理 安装Role-based Authorization St ...

  4. 一文搞定:SpringBoot 集成 Apollo 配置中心

    公众号后台回复"面试",获取精品学习资料 扫描下方海报了解专栏详情 本文来源: http://www.mydlq.club/article/42/ <Java工程师面试突击( ...

  5. SpringBoot 集成 Apollo 配置中心

    目录[-] 目录 一.Kubernetes 部署配置中心 Apollo 二.SpringBoot 集成 Apollo 配置中心 系统环境 SpringBoot 版本:2.1.8.RELEASE Apo ...

  6. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  7. Source Insight之Relation Window Properties配置和一些快捷键

    1 Source Insight之Relation Window Properties配置 我们先点击source Insight的这个地方 然后鼠标右键,点击Relation Window Prop ...

  8. Splunk集成Kafka配置方法

    [摘要]Splunk是业界赫赫有名的数据分析工具,比较擅长BI和安全分析,我司很多部门都有购买其产品和服务.最近有个需求要把Splunk和分部署消息队列Kafka做个集成,Splunk官方提供的一个K ...

  9. Log4j介绍,log4j.properties配置详解

    http://www.cnblogs.com/simle/archive/2011/09/29/2195341.html本文主要解释log4j的配置文件各个配置项的含义,内容是从网上转载的 1.Log ...

  10. SpringCloud Alibaba - Nacos 作为配置中心 读取Properties配置信息

    SpringCloud Alibaba是阿里巴巴致力于对微服务的管理.配置.注册等一整套的解决方案. 简介 Nacos 提供用于存储配置和其他元数据的 K-V 存储,为分布式系统中的外部化配置提供服务 ...

最新文章

  1. 版式设计与创意 pdf_恋爱与版式
  2. Windows 8 JavaScript Metro应用程序--入门(上)
  3. JavaScript学习之ES6学习之Promise
  4. 【java】swing窗口及继承的应用
  5. OpenCV—python—OCR 通用表格自动校正与识别
  6. 【附源码】小白打造「传奇游戏」 (一)
  7. 人脸识别像素最低_一种低分辨率图像的人脸识别方法与流程
  8. 数据的结构分类:结构化数据,半结构化数据以及非结构化数据
  9. Xilinx ISE 千兆以太网通信基础
  10. 通过网页链接直接下载APK
  11. 从OPPO Finder看手机产品的差异化体现
  12. 启示录:了解TOD模式的正确打开方式(一)
  13. 微信公众平台开发 模版消息
  14. Oracle中TO_DATE用法
  15. iPhone 换电池 检测新电池实际容量
  16. 2021高考成绩查询省排名,2021年全国高考难度省份排名 高考最难的省份排名公布...
  17. stm32控制电气比例阀
  18. 25 个 questions, 教你向面试官提问!
  19. Linux中部署redis 以及使用RDM连接redis
  20. Deep Q Network 算法

热门文章

  1. ie11 java8 nc_用命令卸载Win8 IE9/IE10/IE11浏览器
  2. 计算机声卡的作用和功能,声卡有什么功能
  3. 【20保研】厦门大学信息学院2019年全国优秀大学生暑期夏令营招生简章
  4. 基于thinkphp6开放,免费开源可商用的兴趣社区论坛圈子小程序
  5. MVP模式——Okhttp实现下载图片并带有进度 【Android Demo】
  6. 淘宝网图片存储系统架构
  7. 需求分解与需求跟踪矩阵
  8. 支气管炎的饮食要注意哪些
  9. 应用程序无法正常启动0xc0150002+vs2005配置opencv2.2.0
  10. Linux如何安装iperf软件,【iperf】iperfforLinux-最笨下载