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

时间:2022-11-20 00:41:08
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);
     }