How to create java beans dynamically?

百般思念 提交于 2019-12-08 11:59:24

问题


How to create bean/s(retaining column datatype in bean) dynamically after reading columns from table/s in java ?


回答1:


UPDATE Previous answer was an attempt to solve the issue immediately. So it's not the final code for FlexBean. For the final code, visit https://github.com/ramazanpolat/flexbean

OLD ANSWER

I had the same problem so I wrote FlexBean.

public class FlexBean {
    private List<String> propertyNames = new ArrayList<>();
    private Map<String, Object> propertyValueMap = new LinkedHashMap<>();
    private List<Type> propertyTypes  = new ArrayList<>();

    public FlexBean() {

    }

    public void setProperties(Map<String, Object> props){
        for (String propName:props.keySet()){
            addProperty(propName, props.get(propName));
        }
    }


    public void addPropertyNames(List<String> propertyNames){
        for (String name: propertyNames) {
            addProperty(name);
        }
    }

    public Collection<Object> getPropertyValues(){
        return propertyValueMap.values();
    }

    public List<String> getPropertyNames() {
        return propertyNames;
    }

    public Map<String, Object> getPropertyValueMap() {
        return propertyValueMap;
    }

    public List<Type> getPropertyTypes() {
        return propertyTypes;
    }

    public void addProperty(String propName, Type propType){
        propertyNames.add(propName);
        propertyTypes.add(propType);
    }

    public void addProperty(String propName){
        // default property type is String
        addProperty(propName, String.class);
    }

    public void addProperty(String propName, Object value){
        addProperty(propName);
        setProperty(propName, value);
    }

    public void addProperty(String propName, Type propType, Object value){
        addProperty(propName, propType);
        setProperty(propName, value);
    }

    public int getPropertyIndex(String propName){
        return propertyNames.indexOf(propName);
    }

    public Type getPropertyType(String propName){
        int index = getPropertyIndex(propName);
        return Iterables.get(propertyTypes,index);
    }

    public void setProperty(String propName, Object propValue){
        propertyValueMap.put(propName, propValue);
    }

    public Object getPropertyValue(String propName){
        return propertyValueMap.get(propName);
    }

    public <Any> Any getTypedPropertyValue(String propName){
        return (Any)((Any) propertyValueMap.get(propName));
    }

    public Object getProperty(int propIndex){
        return Iterables.get(propertyValueMap.entrySet(),propIndex).getValue();
    }
}

Usage:

FlexBean flexBean = new FlexBean();

flexBean.addProperty("prop1", 1); // int inferred
flexBean.addProperty("prop2", "value2"); // string inferred
flexBean.addProperty("prop3", 0.1f); // float inferred

for (Object o: flexBean.getPropertyValues()) {
    System.out.println(o);
}

int prop1 = flexBean.getTypedPropertyValue("prop1");
String prop2 = flexBean.getTypedPropertyValue("prop2");
float prop3 = flexBean.getTypedPropertyValue("prop3");



回答2:


You can create classes dynamically with some framework like http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/. Alternatively you could store data in a Map where key is column name and value is column value, multiple rows could be stored in a List of Maps




回答3:


You might consider Apache BeanUtils.

It provides several classes for creating beans dynamically. They all implement the DynaBean interface. For example, with the LazyDynaBean:

DynaBean myBean = new LazyDynaBean();
myBean.set("myProperty", "myValue");



回答4:


There're several frameworks/libraries that are available which can help you generate classes. Evgeniy suggested javassist. There're similar libraries like cglib, asm as well that you can explore.

But I would really like to know more about your use-case because in my opinion, dynamically generated classes have their drawbacks in a statically-typed language, such as Java. Also, getting field data types, would require introspection using Java reflection mechanism. That in itself adds to complexity.

I would suggest, either of the following:

  • If you've access to the database schema, consider generating the bean classes beforehand. There're several tools/plugins available for your favorite IDE to achieve this.

  • Consider storing your data in an implementation of the Map interface. For example, you can use one of these to map your column id to the data type and another to map the column id with the data retrieved. You can then use these two maps in your code without the need of creating any beans at runtime.



来源:https://stackoverflow.com/questions/19153930/how-to-create-java-beans-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!