欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

How do I create an instance a JavaBean?

程序员文章站 2022-04-24 14:19:38
...
package org.kodejava.example.bean;

import java.io.Serializable;
import java.io.IOException;
import java.beans.Beans;

public class TheBean implements Serializable {
    private Long id;
    private String name;

    public TheBean() {
    }

    public static void main(String[] args) {
        try {
            TheBean bean = (TheBean) Beans.instantiate(
                    ClassLoader.getSystemClassLoader(), "org.kodejava.example.bean.TheBean");
            System.out.println("The Bean = " + bean);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "[id=" + id + "; name=" + name + "]";
    }
}


转载于:https://my.oschina.net/jsan/blog/57640