当前位置:网站首页>Guice integrated properties configuration
Guice integrated properties configuration
2022-06-11 11:35:00 【Program ape dream factory】
In most projects, we often use reading configuration files , Used to adapt custom attribute values, etc , In this tutorial, we are mainly through the implementation of Properties Based on Guice Configuration resolution for Module.
Based on the environment
technology | edition |
|---|---|
Java | 1.8+ |
Guice | 4.2.3 |
Initialize project
- Initialize project
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-integration-configuration -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
- modify pom.xml increase Guice rely on
<?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 That's the dependency we want to use at the core
structure PropertiesUtils
PropertiesUtils Mainly used for our understanding of Properties Resolution of type file .
- stay
src/main/javaNew under the directory io.edurt.lc.guice.PropertiesUtils Class file , Enter the following in the file
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)));
}
}!!! note
We provide several convenient methods in the tool class :
- `getIntValue` obtain Integer Type data
- `getStringValue` Get string type data
- `getBoolValue` obtain Boolean Type data
- stay
src/main/javaNew under the directory io.edurt.lc.guice.ConfigurationModule Class file , Enter the following in the file
package io.edurt.lc.guice;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.Properties;
public class ConfigurationModule
extends AbstractModule
{
private Properties bootstrapConfiguration;
public ConfigurationModule(Properties bootstrapConfiguration)
{
this.bootstrapConfiguration = bootstrapConfiguration;
}
public ConfigurationModule(String configurationFilePath)
{
if (configurationFilePath != null) {
this.bootstrapConfiguration = PropertiesUtils.loadProperties(configurationFilePath);
}
}
@Override
protected void configure()
{
Names.bindProperties(binder(), bootstrapConfiguration);
}
}Test examples
- The next in
src/test/javadirectories creating io.edurt.lc.guice.TestConfigurationModule Test the service defined by the class file , Add the following code
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;
@Before
public void before()
{
path = this.getClass().getResource("/file.properties").getPath();
injector = Guice.createInjector(new ConfigurationModule(path));
}
@After
public void after()
{
}
/**
* Method: configure()
*/
@Test
public void testConfigure()
{
Assert.assertTrue(injector.getBindings().toString().contains("Test1"));
}
}- stay
test/resourcesNew under the directory file.properties file , The contents are as follows :
name=Test1
We run the program output
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'
Because we use assertions to manipulate , Therefore, after normal operation, the system will not have any input , If there is an error message , Then the console will throw an error message .
Source code address
边栏推荐
- Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:
- 中级web开发工程师,面试题+笔记+项目实战
- 统计出现次数最多的前K个字符串
- WordPress landing page customization plug-in recommendation
- 小白在同花顺上直接开户是安全的吗?
- 数字藏品系统源码搭建
- Cap theory sounds very big, but it's actually very simple
- 在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
- Use yolov5 to train your own data set and get started quickly
- 数字藏品app小程序公众号系统开发
猜你喜欢

An introduction to creating VOC datasets or Yolo datasets using labelimg

导师转我800块,让我仿真一个电路(电源设计)

MyCat-分库分表

JS interview questions - arrow function, find and filter some and every

收货地址列表展示【项目 商城】

使用Yolov5训练自己制作的数据集,快速上手

使用pydub修改wav文件的比特率,报错:C:\ProgramData\Anaconda3\lib\site-packages\pydub\utils.py:170: RuntimeWarning:

Use yolov5 to train your own data set and get started quickly

设置默认收货地址【项目 商城】

Cap theory sounds very big, but it's actually very simple
随机推荐
使用Yolov5训练自己制作的数据集,快速上手
Don't be a fake worker
WordPress站内链接修改插件:Velvet Blues Update URLs
AcWing 1353. 滑雪场设计(贪心)
Method of converting VOC format data set to Yolo format data set
Digital collection app applet official account source code
Iterator mode -- battlefield autumn point
WordPress用户名修改插件:Username Changer
WP super cache static cache plug-in concise tutorial
How to form a good habit? By perseverance? By determination? None of them!
为WordPress相关日志插件增加自动缩略图功能
WP Super Cache静态缓存插件简明使用教程
正大期货主账户预4 周三信息汇总
在毕设中学习02——numpy多维数组的切片,形态变化,维度交换
WordPress regenerate featured image plugin: regenerate thumbnails
Digital collection system app source code
没有财富就不能自由吗?
web开发选型,web开发毕业谁
全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
再不刷题就晚了,最全的BAT大厂面试题整理