java 单例模式防治反攻击

参考: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;

}

}




4:单例的线程安全,经常会在开源的源码中看到:双重检查  懒汉模式
private xxx bean;
public static xxx getInstance(){
  if(bean == null){
synchronized(xxx.class){
if(bean == null){
   bean = new XXX();
                }
}
   
  }
  return bean;
}
这种写法尽量避免了同步带来的性能损耗;

5:反射中的 getDeclaredConstructor();
java.lang.Class.getDeclaredConstructor()  方法返回一个Constructor对象,它反映此Class对象所表示的类或接口指定的构造函数。parameterTypesparameter是确定构造函数的形参类型,在Class对象声明顺序的数组。


6:拓展,饿汉模式我们做了反射验证,那么懒汉模式是否同样有用?

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



测试的结果就是,还是需要加上防止反射的代码;不然反射的时候还是会尝试构建新的对象;

好吧,先纪录着么多;






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值