今天就跟大家聊聊有关使用java怎么动态生成bean,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
java动态的生成javabean,只能生成属性和对应的set/get方法,不能生成其他的方法。
import org.assertj.core.internal.cglib.beans.BeanGenerator;import org.assertj.core.internal.cglib.beans.BeanMap;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class BeanCeater { public static void main(String[] args) throws ClassNotFoundException { System.out.println("Generate JavaBean"); Map properties = new HashMap(); properties.put("id", Class.forName("java.lang.Integer")); properties.put("name", Class.forName("java.lang.String")); properties.put("address", Class.forName("java.lang.String")); Object stu = generateObject(properties); System.out.println("Set values"); setValue(stu, "id", 123); setValue(stu, "name", "454"); setValue(stu, "address", "789"); System.out.println("Get values"); System.out.println(">> " + getValue(stu, "id")); System.out.println(">> " + getValue(stu, "name")); System.out.println(">> " + getValue(stu, "address")); System.out.println("Show all methods"); Method[] methods = stu.getClass().getDeclaredMethods(); for(Method method : methods) { System.out.println(">> " + method.getName()); } System.out.println("Show all properties"); Field[] fields = stu.getClass().getDeclaredFields(); for(Field field : fields) { System.out.println(">> " + field.getName()); } } private static Object generateObject(Map properties) { BeanGenerator generator = new BeanGenerator(); Set keySet = properties.keySet(); for(Iterator i = keySet.iterator(); i.hasNext();) { String key = (String)i.next(); generator.addProperty(key, (Class)properties.get(key)); } return generator.create(); } private static Object getValue(Object obj, String property) { BeanMap beanMap = BeanMap.create(obj); return beanMap.get(property); } private static void setValue(Object obj, String property, Object value) { BeanMap beanMap = BeanMap.create(obj); beanMap.put(property, value); }}
代码不难懂,有需要的自己复制。
补充:spring 工具类 ReflectionUtils 获取bean所有字段
以前遇到要获取当前类以及所有父类的的field的时候,都是递归一直往上找,一直到Object ,个人觉得这种方法是不是太low了,有没有更好的办法?或者jdk其实是有这种方法的,只是我不知道,今天看了下spring中的实现,也是一样的,真没有更好的办法?
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, @Nullable ReflectionUtils.FieldFilter ff) { Class targetClass = clazz; do { Field[] fields = getDeclaredFields(targetClass); Field[] var5 = fields; int var6 = fields.length; for(int var7 = 0; var7 < var6; ++var7) { Field field = var5[var7]; if (ff == null || ff.matches(field)) { try { fc.doWith(field); } catch (IllegalAccessException var10) { throw new IllegalStateException("Not allowed to access field '" + field.getName() + "': " + var10); } } } targetClass = targetClass.getSuperclass(); } while(targetClass != null && targetClass != Object.class); } private static Field[] getDeclaredFields(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); Field[] result = (Field[])declaredFieldsCache.get(clazz); if (result == null) { try { result = clazz.getDeclaredFields(); declaredFieldsCache.put(clazz, result.length == 0 ? NO_FIELDS : result); } catch (Throwable var3) { throw new IllegalStateException("Failed to introspect Class [" + clazz.getName() + "] from ClassLoader [" + clazz.getClassLoader() + "]", var3); } } return result; }
看完上述内容,你们对使用java怎么动态生成bean有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。