在Spring中,由其管理的类都可称为bean,而依赖注入解析bean时,会用到javabeans的规范。
JavaBeans
详细内容见:JavaBeans(TM) Specification 1.01 Final Release
Properties 属性
-
Accessor methods 属性访问器方法
setXx(Xx)
getXx()
-
Indexed properties 索引化的属性
setXx(int, Type)
getXx(int)
-
Bound properties 绑定属性 Xx
对属性Xx绑定事件监听器,属性变更后,发送事件
void addXxListener(PropertyChangeListener p);
void removeXxListener(PropertyChangeListener p);
-
Constrained properties 受限制的属性 Xx
对属性Xx绑定事件监听器,属性变更前,发送事件,事件监听器中可阻止该属性值变更
void addXxListener(VetoableChangeListener p);
void removeXxListener(VetoableChangeListener p);
Methods 方法
Events 事件
EventObject
事件状态对象
EventListener
事件监听器
序列化
详见:java对象序列化规范
示例:
// JavaBean
public class Test1Bean implements Serializable {
private String name;
private String[] datas;
// 访问器属性
public String getName() {
return name;
}
public void setName(String name) {
String oldVal = this.name;
this.name = name;
// 绑定属性
this.propertyChangeSupport.firePropertyChange("name", oldVal, name);
}
public String[] getDatas() {
return datas;
}
public void setDatas(String[] datas) {
this.datas = datas;
}
// 索引化的属性
public void setDatas(int i, String data) throws PropertyVetoException {
// 受限制的属性
this.vetoableChangeSupport.fireVetoableChange("datas", this.datas[i], data);
String oldVal = this.datas[i];
this.datas[i] = data;
// 绑定属性
this.propertyChangeSupport.firePropertyChange("datas", oldVal, data);
}
public String getDatas(int i) {
return datas[i];
}
public int getAge() {
return 0;
}
public void setAge(int name) {
}
public void normalMethod1() {
}
// 事件管理
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private final VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
propertyChangeSupport.addPropertyChangeListener(propertyChangeListener);
}
public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
propertyChangeSupport.removePropertyChangeListener(propertyChangeListener);
}
public void addVetoableChangeListener(VetoableChangeListener vetoableChangeListener) {
vetoableChangeSupport.addVetoableChangeListener(vetoableChangeListener);
}
public void removeVetoableChangeListener(VetoableChangeListener vetoableChangeListener) {
vetoableChangeSupport.addVetoableChangeListener(vetoableChangeListener);
}
}
主要类
获取bean的元数据
JavaBeans元数据.pngIntrospector 内省
解析某个类的元数据,包括该类的属性、方法和事件信息
BeanInfo 元数据
表示某个bean的元数据,包含属性描述、方法描述和事件描述等信息
# 获取bean的元数据
private static void s2_31() throws Exception {
// bean的元数据
final BeanInfo beanInfo = Introspector.getBeanInfo(Test1Bean.class);
// 属性描述信息
final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
log.info("propertyNames[{}]", Arrays.stream(propertyDescriptors).map(PropertyDescriptor::getName).collect(Collectors.toList()));
// 方法描述信息
final MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
log.info("methodNames[{}]", Arrays.stream(methodDescriptors).map(MethodDescriptor::getName).collect(Collectors.toList()));
// 事件描述信息
final EventSetDescriptor[] eventSetDescriptors = beanInfo.getEventSetDescriptors();
log.info("eventNames[{}]", Arrays.stream(eventSetDescriptors).map(EventSetDescriptor::getName).collect(Collectors.toList()));
}
属性编辑器
属性编辑器.pngPropertyEditor 属性编辑器/类型转换器
各种类型和字符串的互相转换
实现自定义的属性编辑器可继承PropertyEditorSupport
类
// 自定义Integer类型的属性编辑器
public class CusIntegerPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setValue(Integer.valueOf(text));
super.setAsText(text);
}
}
PropertyEditorManager 属性编辑器管理者
private static void s2_32() throws FileNotFoundException {
// Integer和字符串的转换
final PropertyEditor propertyEditor = PropertyEditorManager.findEditor(Integer.class);
propertyEditor.setAsText("12");
Integer val = (Integer) propertyEditor.getValue();
log.info("val=[{}]", val);
// 注册自定义的属性编辑器
PropertyEditorManager.registerEditor(Integer.class, CusIntegerPropertyEditor.class);
final PropertyEditor propertyEditor1 = PropertyEditorManager.findEditor(Integer.class);
Integer val1 = (Integer) propertyEditor1.getValue();
log.info("val=[{}]", val);
}
网友评论