java学习-javabeans-customization

Posted 郭星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java学习-javabeans-customization相关的知识,希望对你有一定的参考价值。

java beans specification

9 customization

When a user is composing an application in an application builder we want to allow them to
customize the appearance and behaviour of the various beans they are using.
We allow this customization to occur in two different ways. When a bean exports a set of properties, then an application builder can use these properties to construct a GUI property sheet
that lists the properties and provides a property editor for each property. The user can then use
this property sheet to update the various properties of the bean.
This kind of simple property sheet is likely to work well for simple to medium sized beans.
However for larger and more complex beans, we want to allow more sophisticated kinds of
component customization. For example, we would like to allow component writers to provide
customization “wizards” that guide users through the different steps of customizing a bean,
rather than simply facing them with property sheet choices.
We therefore allow each Java Bean to be accompanied by a customizer class that controls the
customization of the bean. This customizer class should be an AWT component that can be run
to customize a target bean. It can provide whatever GUI behaviour it wishes to control the customization of the target bean

个人理解其通用的含义在于提供通用的可复用的属性操作工具类

 

/*
 * Copyright (c) 2020, guoxing, Co,. Ltd. All Rights Reserved
 */
package com.xingguo.java.beans.customization;

import com.xingguo.java.beans.properties.Person;
import lombok.extern.slf4j.Slf4j;

import java.beans.*;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * PersonCustomizationDemo
 *
 * @author guoxing
 * @date 2020/11/24 9:26 PM
 * @since
 */
@Slf4j
public class PersonCustomizationDemo {

    public static void main(String[] args) throws IntrospectionException {
        // 模拟 <property name="age">18</property>
        Person person = new Person();

        // 通过 内省来获取到所有的property
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        if (Objects.nonNull(propertyDescriptors)) {
            // 对于propertyDescriptor 中的name属性实际就等价于baseName字段
            Stream.of(propertyDescriptors)
                    .filter(propertyDescriptor -> "age".equals(propertyDescriptor.getName()))
                    .findFirst()
                    .ifPresent(propertyDescriptor -> {
                        // 对当前属性设置 自定义 propertyEditor
                        propertyDescriptor.setPropertyEditorClass(PersonAgeEditor.class);
                        PropertyEditor propertyEditor = propertyDescriptor.createPropertyEditor(PersonAgeEditor.class);
                        //对于 propertyEditor 需要增加相应的事件监听器来获取到 setValue/setAsText传递的数据
                        // 对于 propertySupport 中当 set操作完成后会执行 java.beans.PropertyEditorSupport.firePropertyChange 发送事件
                        // 因此需要得到相关的事件信息
                        propertyEditor.addPropertyChangeListener(event -> {
                            // 对于当前操作而言, 因为 对于 propertyEditor 实际只是将外部传递的数据做自定义数据处理并存储,并不支持直接将转换后的数据赋值给要操作的Property所属对象;对于propertyEditor 并没有属于某个对象的含义,其存在的含义是工作类(插件)的作用
                            // 因此对于 真正的对象属性赋值仍然需要手动赋值操作; 因此需要通过监听 propertyEditor#value 属性变更事件来获取到传递的数据("18")
                            person.setAge((int) propertyEditor.getValue());
                        });
                        propertyEditor.setAsText("18");
                    });
            log.info("person.age:{}", person.getAge());
        }
    }
}

 













以上是关于java学习-javabeans-customization的主要内容,如果未能解决你的问题,请参考以下文章

(转)如何学习Java技术?谈Java学习之路

2022年Java学习笔记目录

想学好java,需要学习些啥以及学习步骤是啥

学习java周期与学习方式的关系

Java 学习路线

如何学习Java?学习Java顺序?