BeanUtils { public static <T> void setProperty(Object bean, String name, Class<T> clazz, T value) throws Exception { Method method = ReflectUtils.getPropertySetterMethod(bean.getClass(), name, clazz); if (method.isAccessible()) { method.invoke(bean, value); } else { try { method.setAccessible(true); method.invoke(bean, value); } finally { method.setAccessible(false); } } }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields);  }
@Test public void testSetProperty() throws Exception { TestBean config = new TestBean(); BeanUtils.setProperty(config, "alias", String.class, "1111aaaa"); Assert.assertEquals(config.getAlias(), "1111aaaa"); BeanUtils.setProperty(config, "alias", String.class, null); Assert.assertNull(config.getAlias()); BeanUtils.setProperty(config, "heartbeat", int.class, 3000); Assert.assertEquals(config.getHeartbeat(), 3000); BeanUtils.setProperty(config, "heartbeat", int.class, new Integer(7000)); Assert.assertEquals(config.getHeartbeat(), 7000); boolean error = false; try { BeanUtils.setProperty(config, "xxx", String.class, "1111aaaa"); } catch (Exception e) { error = true; } Assert.assertTrue(error); }