java.beans.Beans 使用demo
/** * @description: demos of jdk8 java.beans.Beans class * java.beans.Beans 作用:主要用来当作初始化bean 的工具,如下 * 内部也是调用了 ClassFinder.findClass(beanName, cls); */ @Test public void testBeans() throws IOException, ClassNotFoundException { User user = (User) Beans.instantiate(this.getClass().getClassLoader(), "com.foundation.one.jdk8.beans.User"); System.out.println(user); }
java.beans.Introspector & java.beans.BeanDescriptor &
java.beans.MethodDescriptor &
java.beans.ParameterDescriptor 使用demo
/** * @description: demos of jdk8 java.beans.Introspector class * java.beans.Introspector 作用:主要用获取beanInfo 的工具, * beanInfo 是类class的描述,是一系列XXXDescriptor()的聚合根 ,省去了自己写反射去获取描述的逻辑 * 下面是BeanDescriptor MethodDescriptor ParameterDescriptor 例子: */ @Test public void testIntrospectorAndBeanInfo() throws IOException, ClassNotFoundException, IntrospectionException { //decapitalize 将大头字母变为小写,如果连续两个大写字母,则不变,类似于spring 初始化bean name的命名规则 String decapitalize1 = Introspector.decapitalize("HaHa"); String decapitalize2 = Introspector.decapitalize("HAHa"); Assert.assertEquals(decapitalize1,"haHa"); Assert.assertEquals(decapitalize2,"HAHa"); //获取com.foundation.one.jdk8.beans.User 类beanInfo信息 BeanInfo beanInfo = Introspector.getBeanInfo(User.class, Introspector.USE_ALL_BEANINFO); //获取BeanDescriptor 相关信息 BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); System.out.println(beanDescriptor.getName()); System.out.println(beanDescriptor.getDisplayName()); System.out.println(beanDescriptor.getBeanClass()); System.out.println(beanDescriptor.getCustomizerClass()); //获取MethodDescriptor 相关信息 MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors(); for (MethodDescriptor methodDescriptor : methodDescriptors) { if (Objects.nonNull(methodDescriptor)) { System.out.println("method:" + methodDescriptor.getName()); //获取ParameterDescriptor 相关信息 ,目前是需要各个系统实现这个方法的,默认方式如下获取会是null ParameterDescriptor parameterDescriptors[] = methodDescriptor.getParameterDescriptors(); //parameterDescriptors 会是null,需要扩展实现,可使用params 获取,如下 Parameter parameters[] = methodDescriptor.getMethod().getParameters(); if (Objects.nonNull(parameters)) { for (Parameter parameter : parameters) { System.out.println("param:" + parameter.getType()); } } } } }
输出结果:
class com.foundation.one.jdk8.beans.User
null
method:getClass
method:setId
param:class java.lang.Integer
method:wait
method:notifyAll
method:getId
method:notify
method:wait
param:long
param:int
method:getUsername
method:hashCode
method:setUsername
param:class java.lang.String
method:wait
param:long
method:equals
param:class java.lang.Object
method:toString
java.beans.PropertyChangeSupport 使用demo
/** * @Author alan.wang * @Description: PropertyChangeSupport 测试类 */ public class PropertyChangeSupportDemo { private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); public void addPropertyChangeListener(PropertyChangeListener listener) { this.pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { this.pcs.removePropertyChangeListener(listener); } private String value; private String id ; public String getValue() { return value; } public String getId() { return id; } public void setId(String id) { this.pcs.firePropertyChange("id",this.id,id); this.id = id; } public void setValue(String value) { this.pcs.firePropertyChange("value",this.value,value); this.value = value; } }
/** * @description: demos of jdk8 java.beans.PropertyChangeSupport class * java.beans.PropertyChangeSupport 作用:主要用帮助类注册PropertyChangeListener 监听器 * PropertyChangeListener 是一个接口,可以实现它作为某类的property 变动时触发的动作 * PropertyChangeSupport ,PropertyChangeListener 例子: */ @Test public void testPropertyChangeSupport(){ PropertyChangeSupportDemo propertyChangeSupportDemo = new PropertyChangeSupportDemo(); propertyChangeSupportDemo.addPropertyChangeListener((e)->{ if(Objects.equals(e.getPropertyName(),"id")) { System.out.println("id old value:" + e.getOldValue()); System.out.println("id new value:" + e.getNewValue()); }else if(Objects.equals(e.getPropertyName(),"value")){ System.out.println("value old value:" + e.getOldValue()); System.out.println("value new value:" + e.getNewValue()); } }); propertyChangeSupportDemo.setId("1"); System.out.println("-------------------break line---------------------------"); propertyChangeSupportDemo.setId("2"); System.out.println("-------------------break line---------------------------"); propertyChangeSupportDemo.setValue("aaa"); System.out.println("-------------------break line---------------------------"); propertyChangeSupportDemo.setValue("bbb"); System.out.println("-------------------break line---------------------------"); }
输出结果 :
id old value:null
id new value:1
-------------------break line---------------------------
id old value:1
id new value:2
-------------------break line---------------------------
value old value:null
value new value:aaa
-------------------break line---------------------------
value old value:aaa
value new value:bbb
-------------------break line---------------------------
java.beans.PropertyChangeListenerProxy 使用demo
/** * @description: demos of jdk8 java.beans.PropertyChangeListenerProxy class * java.beans.PropertyChangeListenerProxy 作用:主要是增强PropertyChangeListener,可以直接注册时绑定属性 * 这样就可以不做属性判定 * PropertyChangeListenerProxy 例子: */ @Test public void testPropertyChangeListenerProxy(){ //可以配合上面例子使用,用来注册回调函数 PropertyChangeSupportDemo propertyChangeSupportDemo = new PropertyChangeSupportDemo(); EventHandlerDemo eventHandlerDemo = new EventHandlerDemo(); //直接添加id属性的listener propertyChangeSupportDemo.addPropertyChangeListener(new PropertyChangeListenerProxy("id",(e -> { System.out.println("id old value:" + e.getOldValue()); System.out.println("id new value:" + e.getNewValue()); }))); //直接添加value属性的listener propertyChangeSupportDemo.addPropertyChangeListener(new PropertyChangeListenerProxy("value",(e -> { System.out.println("value old value:" + e.getOldValue()); System.out.println("value new value:" + e.getNewValue()); }))); propertyChangeSupportDemo.setId("1"); System.out.println("-------------------break line---------------------------"); propertyChangeSupportDemo.setValue("bbb"); }
输出结果 :
id old value:null
id new value:1
-------------------break line---------------------------
value old value:null
value new value:bbb
java.beans.PropertyEditorManager
java.beans.PropertyEditor 使用demo
/** * @Author alan.wang * @description: PropertyEditor 偶数类 */ public class EvenNumber { public static final Class<EvenNumber> TYPE = EvenNumber.class; }
/** * @Author alan.wang * 进入后都会变成偶数,如果本身是偶数,则返回本身,如果不是偶数,则+1 返回 */ public class EvenNumberPropertyEditorDemo extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { Integer evenNumber = Integer.parseInt(text); if(evenNumber%2==0){ setValue(evenNumber); }else { setValue(evenNumber+1); } } }
/** * @description: demos of jdk8 java.beans.PropertyEditor class * java.beans.PropertyEditor 作用:主要用是对一个值进行转换,变为另一个类型或者另一个值 * 以下是对一个数进行转换,如果是偶数则返回本身,不是偶数则+1 返回 * PropertyEditorManager PropertyEditor 例子: */ @Test public void testPropertyEditor(){ PropertyEditorManager.registerEditor(EvenNumber.TYPE,EvenNumberPropertyEditorDemo.class); EvenNumberPropertyEditorDemo evenNumberPropertyEditorDemo = (EvenNumberPropertyEditorDemo)PropertyEditorManager.findEditor(EvenNumber.TYPE); evenNumberPropertyEditorDemo.addPropertyChangeListener((evt -> { System.out.println(evenNumberPropertyEditorDemo.getValue()); })); evenNumberPropertyEditorDemo.setAsText("100"); evenNumberPropertyEditorDemo.setAsText("101"); }
输出结果:
100
102
java.beans.EventHandler 使用demo
/** * @Author alan.wang * @Desctiption: EventHandler demo 回调类 */ public class EventHandlerDemo { public void print(String value) { System.out.println(value); } }
/** * @description: demos of jdk8 java.beans.EventHandler class * java.beans.EventHandler 作用:主要用帮助类创建一个回调类,节省了手动写注入并执行方法的代码 * EventHandler 例子: */ @Test public void testEventHandler(){ //可以配合上面例子使用,用来注册回调函数 PropertyChangeSupportDemo propertyChangeSupportDemo = new PropertyChangeSupportDemo(); EventHandlerDemo eventHandlerDemo = new EventHandlerDemo(); //不用EventHandler常规实现 propertyChangeSupportDemo.addPropertyChangeListener(evt -> { eventHandlerDemo.print((String) evt.getNewValue()); }); propertyChangeSupportDemo.setValue("aaa"); //EventHandler实现 propertyChangeSupportDemo.addPropertyChangeListener( EventHandler.create(PropertyChangeListener.class,eventHandlerDemo,"print","newValue")); propertyChangeSupportDemo.setValue("bbb"); }