11.1 Propert Editor
- property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如
2014-03-02
和Date
类型的互相转换) - spring内置了CustomDateEditor, CustomNumberEditor, ClassEditor, FileEditor, LocaleEditor, StringArrayPropertyEditor
- 除了内置的property editor,如需自己定制额外的复杂情况继承JavaBeans API的PropertyEditorSupport类
11.2 示例
11.2.1 使用Spring内置的Editor
11.2.1.1 编写演示bean
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DemoBean {
@Value("2014/02/03")
private Date demoDate;
public Date getDemoDate() {
return demoDate;
}
public void setDemoDate(Date demoDate) {
this.demoDate = demoDate;
}
}
11.2.1.2 编写配置
package com.wisely.propertyeditor;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DemoConfig {
@Bean
public CustomDateEditor dateEditor(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
return new CustomDateEditor(dateFormat, true);
}
}
11.2.1.3 测试
package com.wisely.propertyeditor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.propertyeditor");
DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getDemoDate());
context.close();
}
}
输出结果
Mon Feb 03 00:00:00 CST 2014
11.2.2 使用PropertyEditorSupport
11.2.2.1 编写需要和字符转换的javabean
此为传值对象,不需要声明称spring的bean
package com.wisely.propertyeditor;
public class DemoBean2 {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
11.2.2.2 在DemoBean中注入该bean
package com.wisely.propertyeditor;
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DemoBean {
@Value("汪云飞-合肥")
private DemoBean2 demoBean2;
public DemoBean2 getDemoBean2() {
return demoBean2;
}
public void setDemoBean2(DemoBean2 demoBean2) {
this.demoBean2 = demoBean2;
}
}
11.2.2.3 实现自定义的Property Editor
package com.wisely.propertyeditor;
import java.beans.PropertyEditorSupport;
public class DemoPropertyEditor extends PropertyEditorSupport{
@Override
public String getAsText() {
DemoBean2 bean2 =(DemoBean2) getValue();
return bean2.getClass().getName() + "," + bean2.getName()
+ "," + bean2.getAddress();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] parts = text.split("-");
try{
DemoBean2 bean2 = new DemoBean2();
bean2.setName(parts[0]);
bean2.setAddress(parts[1]);
setValue(bean2);
}catch(Exception e){
throw new IllegalArgumentException(e);
}
}
}
11.2.2.4 配置editorConfigurer
package com.wisely.propertyeditor;
import java.beans.PropertyEditor;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Demo2Config {
@Bean
public CustomEditorConfigurer editorConfigurer(){
CustomEditorConfigurer editorConfigurer = new CustomEditorConfigurer();
Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>();
customEditors.put(DemoBean2.class, DemoPropertyEditor.class);
editorConfigurer.setCustomEditors(customEditors);
return editorConfigurer;
}
}
11.2.2.5 测试
package com.wisely.propertyeditor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.propertyeditor");
DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getDemoBean2().getName()+"///"
+demoBean.getDemoBean2().getAddress());
context.close();
}
}
输出结果
汪云飞///合肥
相关推荐
国元点睛-20230825-国元期货-23页
眼睛穴位图-睛明穴-取穴方法 方便经常上网的朋友使用
六爱护眼睛---复式统计图(第1课时).doc
图像与眼睛(2009-04-1.pdf
让消费者眼睛亮起来--稀世宝矿泉水整合营销策划案例(doc 20).doc
包含了谷歌眼睛的全部核心代码,可以用记事本、Dreamweaver、写字板、html等打开
这是一款保护眼睛的小软件,绿色软件,不需安装即可使用,试一下吧。
[最新]八年级上册物理:5.4-眼睛和眼镜-精品教案.pdf
闭着眼睛记单词-英语基础《英语1000高频词汇》
使用OpenCV1.0实现了眼睛状态识别,首先用harr检测眼睛,然后用模板匹配的方法标记眼睛状态。 程序需要使用Eyes45x11.xml Mouth25x15.xml Nose25x15.xml,将其放到C:\Program Files\OpenCV\data\haarcascades目录下...
opencv用于检测带眼镜的眼睛haarcascade_eye_tree_eyeglasses.xml
上网保护眼睛--网页底色修改.2011-11-25最新整理,2011-11-25最新整理...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
opencv用于检测眼睛。haarcascade_eye.xml文件
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
点睛-文本各种编码方式转变.7z
软件名:点睛文本编码查询 功能:本软件用于将文本的字符串转换为 Text、ASCII、Default、Unicode、BigEndianUnicode、UTF-8、UTF-7、GBK、BIG5 之间的互相转换,用于文本查询。转换结果使用十六进制表示。
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
4.1人的眼睛.pdf