新增: properties 配置读取工具类方法
parent
12a16394c4
commit
1b72b1924e
@ -0,0 +1,37 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>space.caoshd</groupId>
|
||||
<artifactId>otone</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>8.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.4.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,44 @@
|
||||
package space.caoshd.otone.tool;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* properties 加载类
|
||||
*/
|
||||
public class PropertyTools {
|
||||
|
||||
Map<String, String> propertyMap;
|
||||
|
||||
public PropertyTools(InputStream stream) {
|
||||
propertyMap = loadProperties(stream);
|
||||
}
|
||||
|
||||
public Map<String, String> loadProperties(InputStream stream) {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.load(stream);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
properties.forEach((k, v) -> result.put((String) k, (String) v));
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String key) {
|
||||
return propertyMap.get(key);
|
||||
}
|
||||
|
||||
public Integer getInt(String key) {
|
||||
return Integer.valueOf(propertyMap.get(key));
|
||||
}
|
||||
|
||||
public Long getLong(String key) {
|
||||
return Long.valueOf(propertyMap.get(key));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package space.caoshd.otone.util;
|
||||
|
||||
public class Constants {
|
||||
public Constants() {
|
||||
}
|
||||
|
||||
public static final String DATASOURCE_PATH = "datasource.properties";
|
||||
|
||||
public static final String CONFIG_PATH = "config.properties";
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package space.caoshd.otone.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
public class ResourceUtils {
|
||||
public static InputStream getClassPathFileStream(String filename) {
|
||||
return ResourceUtils.class.getClassLoader().getResourceAsStream(filename);
|
||||
}
|
||||
|
||||
public static File getClassPathFile(String filename) {
|
||||
URL resource = ResourceUtils.class.getClassLoader().getResource(filename);
|
||||
assert resource != null;
|
||||
return new File(resource.getFile());
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
username=root
|
||||
password=123456
|
||||
url=jdbc:mysql://localhost:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
driver-class-name=com.mysql.cj.jdbc.Driver
|
@ -0,0 +1,34 @@
|
||||
package space.caoshd.otone.tool;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import space.caoshd.otone.util.Constants;
|
||||
import space.caoshd.otone.util.ResourceUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PropertyToolsTest {
|
||||
|
||||
@Test
|
||||
void loadProperties() {
|
||||
InputStream fileStream = ResourceUtils.getClassPathFileStream(Constants.DATASOURCE_PATH);
|
||||
PropertyTools propertyTools = new PropertyTools(fileStream);
|
||||
Assertions.assertNotNull(propertyTools);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getString() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getInt() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLong() {
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package space.caoshd.otone.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
class ResourceUtilsTest {
|
||||
|
||||
@Test
|
||||
void getClassPathFile() {
|
||||
File file = ResourceUtils.getClassPathFile("datasource.properties");
|
||||
System.out.println(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getClassPathFileStream() {
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
username=root
|
||||
password=123456
|
||||
url=jdbc:mysql://localhost:3306/otone?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
|
||||
driver-class-name=com.mysql.cj.jdbc.Driver
|
Loading…
Reference in New Issue