|
@@ -0,0 +1,102 @@
|
|
1
|
+package kr.co.swh.lecture.java.example;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.io.InputStream;
|
|
5
|
+import java.util.Properties;
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * <pre>
|
|
9
|
+ * kr.co.swh.lecture.java.example
|
|
10
|
+ * PropertiesDemo.java
|
|
11
|
+ *
|
|
12
|
+ * 설명 : Properties 예제
|
|
13
|
+ * </pre>
|
|
14
|
+ *
|
|
15
|
+ * @since : 2017. 6. 28.
|
|
16
|
+ * @author : tobby48
|
|
17
|
+ * @version : v1.0
|
|
18
|
+ */
|
|
19
|
+public class PropertiesDemo {
|
|
20
|
+ private Properties properties;
|
|
21
|
+
|
|
22
|
+ public PropertiesDemo() {
|
|
23
|
+ properties = new Properties();
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ public Properties getProperties() {
|
|
27
|
+ return properties;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ /**
|
|
31
|
+ * 이 메소드는 프로퍼티 파일을 스트림으로 읽어 들여 멤버 변수의 프로퍼티에 적재합니다.
|
|
32
|
+ *
|
|
33
|
+ * @param path
|
|
34
|
+ * @throws IOException
|
|
35
|
+ */
|
|
36
|
+ public void loadProp(String path) throws IOException {
|
|
37
|
+ InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
|
38
|
+ properties.load(inputStream);
|
|
39
|
+ inputStream.close();
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ /**
|
|
43
|
+ * 이 메소드는 static으로 선언해서 프로퍼티 파일을 읽는 것을 보여줍니다.
|
|
44
|
+ * @param path
|
|
45
|
+ * @return
|
|
46
|
+ * @throws IOException
|
|
47
|
+ */
|
|
48
|
+ public static Properties loadPropForStatic(String path) throws IOException {
|
|
49
|
+ Properties properties = new Properties();
|
|
50
|
+ InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
|
51
|
+ properties.load(inputStream);
|
|
52
|
+ inputStream.close();
|
|
53
|
+ return properties;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public static void main(String[] args) throws IOException {
|
|
57
|
+ PropertiesDemo propertiesDemo = new PropertiesDemo();
|
|
58
|
+
|
|
59
|
+ // 프로퍼티 파일을 읽어들이고 해당 값을 출력해봅니다.
|
|
60
|
+ propertiesDemo.loadProp("application.properties");
|
|
61
|
+ Properties properties = propertiesDemo.getProperties();
|
|
62
|
+ properties.list(System.out);
|
|
63
|
+
|
|
64
|
+ // 아래 코드는 새로운 프로퍼티 파일에 같은 키를 준 경우 오버라이드 하는 것을 확인합니다.
|
|
65
|
+ Properties staticProp = PropertiesDemo.loadPropForStatic("application-prod.properties");
|
|
66
|
+ properties.putAll(staticProp);
|
|
67
|
+ properties = propertiesDemo.getProperties();
|
|
68
|
+ properties.list(System.out);
|
|
69
|
+
|
|
70
|
+ // 아래 코드는 프로퍼티간의 결합을 확인합니다.
|
|
71
|
+ Properties dummy = new Properties();
|
|
72
|
+ dummy.put("demo.type", "dummy"); // 기존 키를 오버라이드 합니다.
|
|
73
|
+ dummy.put("demo.temp", "temp"); // 새로운 키를 추가합니다.
|
|
74
|
+ properties.putAll(dummy); // 기존 프로퍼티에 더미 프로퍼티를 결합합니다.
|
|
75
|
+ properties.list(System.out);
|
|
76
|
+
|
|
77
|
+ // 아래 코드는 개별 키를 주어 값을 반환받습니다.
|
|
78
|
+ Object type = properties.get("demo.type");
|
|
79
|
+
|
|
80
|
+ // 아래코드는 프로퍼티를 키들을 순회하는 구문입니다.
|
|
81
|
+ // .stringPropertyNames 대신 .keySet 을 사용할수도 있습니다.
|
|
82
|
+ for (String key : properties.stringPropertyNames()) {
|
|
83
|
+ Object value = properties.getProperty(key);
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ // 해당 키가 있는지 여부를 판별합니다.
|
|
87
|
+ properties.containsKey("demo.type");
|
|
88
|
+
|
|
89
|
+ // 해당 값이 있는지 여부를 판별합니다.
|
|
90
|
+ properties.containsValue("dummy");
|
|
91
|
+
|
|
92
|
+ // 키값을 주고 해당 값이 있으면 해당 값을 반환하지만
|
|
93
|
+ // 해당 값이 없으면 null을 반환합니다.
|
|
94
|
+ Object result = properties.computeIfAbsent("undefined", value -> returnNull(value));
|
|
95
|
+ System.out.println("result value is " + result);
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ public static Object returnNull(Object key) {
|
|
99
|
+ System.out.println(key + " value is null.");
|
|
100
|
+ return null;
|
|
101
|
+ }
|
|
102
|
+}
|