反射的更改 更改 static final 静态常量的值 。 这样就能动态的更改 决定spring 用那一套实现

时间:2015-05-18 16:29:12   收藏:0   阅读:175
public class Bean {
 
    public static final String dbtype=new String();
 
}
 
 
public static  void changFinalProperty(Class clazz ,Object newvalue)
    {
         
        try {
            Object o = clazz.newInstance();
            Field  field = clazz.getDeclaredField("dbtype");
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.set(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, newvalue);
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        System.out.println(Bean.dbtype);
    }
    

原文:http://www.cnblogs.com/happyxiaomo/p/4512106.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!