java学习-javabeans-customization
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java学习-javabeans-customization,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3157字,纯文字阅读大概需要5分钟。
内容图文
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 publicclass PersonCustomizationDemo { publicstaticvoid 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()); } } }
原文:https://www.cnblogs.com/xingguoblog/p/14033098.html
内容总结
以上是互联网集市为您收集整理的java学习-javabeans-customization全部内容,希望文章能够帮你解决java学习-javabeans-customization所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。