2

我使用 JAXB XJC 创建了一些类。它们遵循这种模式(一些带有 getter 和 setter 以及 xml 注释的属性):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "School", propOrder = {
    "info",
    "students"
})
public class School {

    @XmlElement(name = "Info", required = true)
    protected Info info;

    @XmlElement(name = "Students", required = true)
    protected List<Student> students;

    public Info getInfo(){ return info;}

    public void setInfo(Info value){ info = value};

    public List<Students> getStudents(){ 
       if (students == null) {
            students = new ArrayList<Student>();
        }
       return students;
    }

    public void setStudents(List<Student> elements){
       if (students == null) {
            students = new ArrayList<Student>();
        }
       students.addAll(elements);
    } 

当我使用java.beans.Introspector.getBeanInfoandBeanInfo.getPropertyDescriptors获取类 BeanInfo 时,我可以看到我的School类有一个名为beingstudents的属性。我相信这个类的 BeanInfo 是按照 Bean 的默认规则生成的,根据 Java Beans 规范WriteMethodsetStudents(List)

如果我们发现一对匹配的get<PropertyName>set<PropertyName>方法接受和返回相同的类型,那么我们认为这些方法定义了一个读写属性,其名称将为<propertyName>

那么,如何改变这个类的 BeanInfo 呢?更准确地说,我想将WriteMethod属性students的 设置为另一种不遵循上述默认规则的方法(它将是public void setStudent (Student student){...})。我怎样才能做到这一点?

只是为了确保清楚...当我使用java.beans.Introspector.getBeanInfoBeanInfo.getPropertyDescriptors获取类 BeanInfo时,我希望看到我的School类具有作为我的新 set 方法的属性(遵循 默认规则)。studentsWriteMethodsetStudents(Student)

我在这里看到了一些东西,但这对我来说还不够......

谢谢!

4

0 回答 0