11.1 Propert Editor

  • property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02Date类型的互相转换)
  • 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();
}
}

输出结果

汪云飞///合肥

11点睛Spring4.1-Property Editor的更多相关文章

  1. activemq5.11整合spring4.2.3

    前言 这篇博客记录 activemq5.11整合spring4.2.3的过程,免得以后忘记了 1.工程结构 2.pom.xml <project xmlns="http://maven ...

  2. How to use umbraco datetime property editor

    When I was using Umbraco datetime property editor, I met with a problem that the editor must be firs ...

  3. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  4. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  5. 第15.11节 PyQt(Python+Qt)入门学习:Qt Designer(设计师)组件Property Editor(属性编辑)界面中主窗口QMainWindow类相关属性详解

    概述 主窗口对象是在新建窗口对象时,选择main window类型的模板时创建的窗口对象,如图: 在属性编辑界面中,主窗口对象与QMainWindow相关的属性包括:iconSize.toolButt ...

  6. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  7. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  8. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  9. XAF Spreadsheet property Editor

    https://www.devexpress.com/Support/Center/Question/Details/T371232

随机推荐

  1. C# 异步编程(async&await)

    同步:同步就是指一个进程在执行某个请求的时候,若该请求需要一段时间才能返回信息,那么这个进程将会一直等待下去,直到收到返回信息才继续执行下去 异步:异步是指进程不需要一直等下去,而是继续执行下面的操作 ...

  2. mount命令设备的挂载和卸载

    不能挂载到根目录:其他目录都可以 1.光盘设备挂载到mnt mount -t iso99660 /dev/cdrom /mnt 2.光盘镜像文件.so文件挂载到mnt mount -o loop -t ...

  3. 83: 模拟赛 树形dp

    $des$ $sol$ 维护每个点的子树中的信息以及非子树的信息 $code$ #include <bits/stdc++.h> using namespace std; #define ...

  4. mpvue搭建小程序框架

    http://mpvue.com/mpvue/ 美团开源了mpvue 由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler) 运行时框架 runtime 和代码编译器 c ...

  5. UOJ272. 【清华集训2016】石家庄的工人阶级队伍比较坚强 [FWT]

    UOJ 思路 很容易想到\(O(3^{3m}\log T)\)的暴力大矩乘,显然过不了. 我们分析一下每次转移的性质.题目给的转移方程是填表法,我们试着改成刷表法看看-- 发现好像没啥用. 注意到游戏 ...

  6. 深度Linux /etc/profile 环境变量生效问题

    /etc/profile 环境变量生效问题 设置了环境变量后 ,使用source /etc/profile生效后,每次关闭终端后,都需要重新输入source /etc/profile命令使环境变量生效 ...

  7. [提权]mysql中的UDF提权

    由于udf提权是需要构造UDF函数文件的,涉及到了写文件.所以本次实验已经将mysql的配置做了改动:–secure-file-priv=''. 剧情须知: secure_file_priv 为 NU ...

  8. redis主从复制读写分离

    主从复制,读写分离 Master/Slave 是什么 master写入 slave读取 能干嘛 读写分离,更加安全,性能提升 怎么玩 一主二仆.薪火相传.反客为主 周明老师,能够把长篇大论总结的很精辟 ...

  9. T-MAX组--项目冲刺(第六天)

    T-MAX组--项目冲刺(第六天) THE SIXTH DAY 项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 T-MAX组--项 ...

  10. The difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data

    重构:改善饿了么交易系统的设计思路 原创: 盛赫 阿里巴巴中间件 昨天