6.
7.
private
static
DBConfig dbconfig =
null
;
8.
9.
private
Properties props =
null
;
10.
11.
public
static
DBConfig getInstance() {
12.
if
(dbconfig ==
null
) {
13.
synchronized
(initLock) {
14.
if
(dbconfig ==
null
) {
15.
dbconfig =
new
DBConfig();
16. }
17. }
18. }
19.
return
dbconfig;
20. }
21.
22.
private
synchronized
void
loadProperties() {
23.
props =
new
Properties();
24.
try
{
25.
System.out.println(
"Load pro file"
);
26.
InputStream in = getClass().getResourceAsStream(
"/db.
p
roperties
"
);
27. props.load(in);
28.
}
catch
(Exception e) {
29. e.printStackTrace();
30. }
31. }
32.
33.
public
String getProperty(String propName) {
34.
if
(props ==
null
) {
35. loadProperties();
36. }
37.
return
props.getProperty(propName);
38. }
39. }
Java
代码
1.
import
java.util.Properties;
2.
import
java.io.*;
3.
4.
public
class
DBConfig {
5.
private
static
Object initLock =
new
Object();