参考:http://m.blog.youkuaiyun.com/article/details?id=50525335
1: single bean 饿汉模式
package com.xxx.cn;
/**
*
* @author petter
*
*/
public class TestSingleBean {
private static boolean flag = false;
public static final TestSingleBean bean = new TestSingleBean();
private TestSingleBean(){
if(flag == false){
flag = !flag;
}else{
throw new RuntimeException("构件单列异常");
}
}
// public static TestReflectionBean getInstance(){
// if(bean == null){
// bean = new TestReflectionBean();
// }
// return bean;
// }
public void test(){
System.out.println("test reflection");
}
}
2:测试代码:
public class HelloWorld {
public static void main(String args[]){
System.out.println("JAVA project");
testReflactionBeanMethod();
testReflactionMethond();
}
private static void testReflactionBeanMethod(){
try {
//正常情况
TestSingleBean testbean = TestSingleBean.bean;
System.out.println(testbean.hashCode());
TestSingleBean testbean2 = TestSingleBean.bean;
System.out.println(testbean2.hashCode());
//设置setAccessible属性
Class<?> classType = TestSingleBean.class;
Constructor<?> c1 = classType.getDeclaredConstructor(null);
c1.setAccessible(true);//添加此方法设置属性,可以跳用private构造函数;accessible 可以进入的意思
TestSingleBean e1 = (TestSingleBean)c1.newInstance();
System.out.println(e1.hashCode());
//反射构建对象
Class c = Class.forName("com.xxx.cn.TestSingleBean");
Constructor c0=c.getDeclaredConstructor();
c0.setAccessible(true);
TestSingleBean po=(TestSingleBean)c0.newInstance();
po.test();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void testReflactionMethond(){
String str = "123";
TestReflectionMethod.change(str);
System.out.println(str);
}
}
3:打印结果
JAVA project
104181190
104181190
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at petter.HelloWorld.testReflactionBeanMethod(HelloWorld.java:25)
at petter.HelloWorld.main(HelloWorld.java:11)
Caused by: java.lang.RuntimeException: 构件单列异常
at com.xxx.cn.TestSingleBean.<init>(TestSingleBean.java:15)
... 6 more
123
说明:(了解)
在1,2,3,公有静态成员是个final域;
如果构造方法改造为:公有的成员是个静态工厂方法:
public class xxx{
private static final xxx INSTANCE = new xxx();
private xxx(){};
public static xxx getInstance(){
return INSTANCE;
}
//防止序列化生成新的实例
private object redResolve(){
return INSTANCE;
}
}
package com.xxx.cn;
/**
*
* @author petter
*
*/
public class TestSingleBean {
private static boolean flag = false;
private static TestSingleBean bean;
// public static final TestSingleBean bean = new TestSingleBean();
private TestSingleBean(){
if(flag == false){
flag = !flag;
}else{
throw new RuntimeException("构件单列异常");
}
}
public static TestSingleBean getInstance(){
if(bean == null){
bean = new TestSingleBean();
}
return bean;
}
}
JAVA project
1367113803
1367113803
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at petter.HelloWorld.testReflactionBeanMethod(HelloWorld.java:27)
at petter.HelloWorld.main(HelloWorld.java:11)
Caused by: java.lang.RuntimeException: 构件单列异常
at com.xxx.cn.TestSingleBean.<init>(TestSingleBean.java:16)
... 6 more
123
测试的结果就是,还是需要加上防止反射的代码;不然反射的时候还是会尝试构建新的对象;