zoukankan      html  css  js  c++  java
  • 反射的更改 更改 static final 静态常量的值 。 这样就能动态的更改 决定spring 用那一套实现

    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);
        }
        
  • 相关阅读:
    使用GitHub+hexo搭建个人独立博客
    HDU 3038
    POJ 1417
    HDU 1213
    ZOJ 3781
    ZOJ 3780
    ZOJ 3777
    HDU 3045
    HDU 3480
    POJ 1180
  • 原文地址:https://www.cnblogs.com/happyxiaomo/p/4512106.html
Copyright © 2011-2022 走看看