package com.xzit.util;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class Property {
private Property(){
}
private static final Property prop = new Property();
public static Property getInstance(){
return prop;
}
private InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
private static final Properties loadFile(){
Properties props = new Properties();
try {
props.load(prop.is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props;
}
public static final String getDRIVER(){
return loadFile().getProperty("driver");
}
public static final String getURL(){
return loadFile().getProperty("url");
}
public static final String getUSERNAME(){
return loadFile().getProperty("uname");
}
public static final String getPASSWORD(){
System.out.println("zz");
// System.out.println(loadFile().getProperty("pwd"));
// System.out.println("=============");
return loadFile().getProperty("pwd");
}
public static void main(String[] args) {
System.out.println(getPASSWORD());
System.out.println(getPASSWORD());
System.out.println(getPASSWORD());
}
}
不过正如1楼所说,你例子代码中根本没用上单例,你只是使用了静态方法,和单例无关。要使用单例,一定是通过getInstance方法获取类的唯一实例,然后通过实例方法来实现逻辑。
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Property {
private Properties props = null;
private Property(){
loadFile();
}
public static final Property Instance = new Property();
private InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
private final void loadFile(){
Properties props = new Properties();
try {
props.load(prop.is);
this.props = props;
} catch (IOException e) {
e.printStackTrace();
}
}
public final String getDRIVER(){
return this.props.getProperty("driver");
}
public final String getURL(){
return this.props.getProperty("url");
}
public final String getUSERNAME(){
return this.props.getProperty("uname");
}
public final String getPASSWORD(){
System.out.println("zz");
// System.out.println(loadFile().getProperty("pwd"));
// System.out.println("=============");
return this.props.getProperty("pwd");
}
public static void main(String[] args) {
System.out.println(Property.Instance.getPASSWORD());
System.out.println(Property.Instance.getPASSWORD());
System.out.println(Property.Instance.getPASSWORD());
}
}
package com.xzit.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Property {
private Properties props = null;
private Property(){
loadFile();
}
public static final Property Instance = new Property();
private final void loadFile(){
Properties props = new Properties();
InputStream is = null;
try {
InputStream is = this.getClass().getResourceAsStream("/jdbc.properties");
props.load(prop.is);
this.props = props;
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is != null) {
try{ is.close(); } catch(IOException e){ e.printStackTrace(); }
}
}
}
public final String getDRIVER(){
return this.props.getProperty("driver");
}
public final String getURL(){
return this.props.getProperty("url");
}
public final String getUSERNAME(){
return this.props.getProperty("uname");
}
public final String getPASSWORD(){
System.out.println("zz");
// System.out.println(loadFile().getProperty("pwd"));
// System.out.println("=============");
return this.props.getProperty("pwd");
}
public static void main(String[] args) {
System.out.println(Property.Instance.getPASSWORD());
System.out.println(Property.Instance.getPASSWORD());
System.out.println(Property.Instance.getPASSWORD());
}
}
private static OnlyOne oo = null;
/*private:禁止外界直接更改
* static:只能有一个对象,所以刚开始必须用类名调用
*/
private OnlyOne() {
super();
}
//不允许外界直接初始化
public static OnlyOne init() {
if (oo == null)
oo = new OnlyOne();
return oo;
}
/*开放特殊的初始化方法
*/
public static void main(String[] args) {
OnlyOne ootest=OnlyOne.init();
OnlyOne oo2=OnlyOne.init();
System.out.println(ootest==oo2);
System.out.println(ootest.equals(oo2));
}}
http://www.javafor.com/articles/2016/08/26/1472190589614.html