?1
/**?*//**
?2
?*?全局變量的單例模式,使用eager?instance。
?3
?*?從指定的配置文件中讀取配置信息,并將配置信息儲存到properties屬性。
?4
?*?提供訪問屬性的方法,不提供修改屬性的方法。
?5
?*/
?6
?7
import?java.io.FileInputStream;
?8
import?java.io.FileNotFoundException;
?9
import?java.io.IOException;
10
import?java.util.HashMap;
11
import?java.util.Properties;
12
13
public?class?RuntimeConstants?
14

{
15
??private?final?static?String?_CONF_FILE_NAME="d:\\conf.properties";
16
??private?static?RuntimeConstants?_instance=new?RuntimeConstants();
17
??private?HashMap?properties=new?HashMap();
18
??
19
??private?RuntimeConstants()
20
??
{
21
????init();
22
??}
23
24
??public?static?void?main(String[]?args)
25
??
{
26
????System.out.println((String)RuntimeConstants.getInstance().getProperty("log.level"));
27
????RuntimeConstants.getInstance().setProperty("log.level","DEBUG5555555");
28
????System.out.println((String)RuntimeConstants.getInstance().getProperty("log.level"));
29
??}
30
??
31
??/**?*//**
32
???*?獲取單例實例。
33
34
???*/
35
??public?static?RuntimeConstants?getInstance()
36
??
{
37
????return?_instance;
38
??}
39
??
40
??/**?*//**
41
???*?從指定的配置文件讀取配置信息,并裝配到properties屬性
42
???*/
43
??private?void?init()
44
??
{
45
????Properties?p=new?Properties();
46
????try
47
????
{
48
??????p.load(new?FileInputStream(_CONF_FILE_NAME));
49
??????Object[]?keys=p.keySet().toArray();
50
??????int?i=0;
51
??????for(i=0;i<keys.length;i++)
52
??????
{
53
????????properties.put((String)keys[i],p.getProperty((String)keys[i]));
54
??????}
55
????}
56
????catch?(FileNotFoundException?e)
57
????
{
58
??????System.out.println("[ERROR]?沒有找到配置文件?"+e);
59
????}
60
????catch?(IOException?e)
61
????
{
62
??????System.out.println("[ERROR]?讀取文件失敗?"+e);
63
????}
64
????p.clear();
65
??}
66
??
67
??public?Object?getProperty(Object?key)
68
??
{
69
????return?properties.get(key);
70
??}
71
72
??public?void?setProperty(Object?key,?Object?value)
73
?
{
74
??????properties.put(key,value);
75
?}
76
77
}
78


?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14



15

16

17

18

19

20



21

22

23

24

25



26

27

28

29

30

31


32

33

34

35

36



37

38

39

40


41

42

43

44



45

46

47



48

49

50

51

52



53

54

55

56

57



58

59

60

61



62

63

64

65

66

67

68



69

70

71

72

73



74

75

76

77

78

conf.properties 如下:
?1
db.target.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
?2
db.target.user=sa
?3
db.target.password=123456
?4
db.target.schema=soman
?5
db.target.url=
?6
db.target.connections=10
?7
?8
db.source.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
?9
db.source.user=sa
10
db.source.password=654321
11
db.source.schema=somanQuery
12
db.source.url=
13
db.source.connections=20
14
15
runtime.thread.max=50
16
17
log.level=DEBUG

?2

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17
