/**
* 初始化数据
* @param bean
* @param bean
* @return
*/
public static <T> T getRequestBody(T bean) throws IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException {
log.info("LoginContext.getRequestBody() strat bean:{}", JSON.toJSONString(bean));
BeanInfo beanInfo;
//throws IntrospectionException
beanInfo= Introspector.getBeanInfo(bean.getClass(), Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor p:propertyDescriptors){
if("tenantId".equals(p.getName())){
//调用该属性名称对应的setter方法
//throws IntrospectionException,InvocationTargetException,IllegalAccessException
new PropertyDescriptor(p.getName(), bean.getClass()).getWriteMethod().invoke(bean, getTenantId());
}
if ("creator".equals(p.getName())|| "modifier".equals(p.getName())){
new PropertyDescriptor(p.getName(), bean.getClass()).getWriteMethod().invoke(bean, getCurrentPin());
}
}
log.info("LoginContext.getRequestBody() end bean:{}", JSON.toJSONString(bean));
return bean;
}
bean反射替换属性值
最新推荐文章于 2022-08-27 17:34:47 发布