Spring之PropertyEditor

1 理论知识

http://stamen.iteye.com/blog/1525668
http://layznet.iteye.com/blog/908566

2 代码实现

Worker.java
package study.property;

public class Worker {
	private int id;
	private String name;
	private int salary;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "worker[id:"+id+",name:"+name+",salary:"+salary+"]";
	}

}
WorkerEditor.java
package study.property;

import java.beans.PropertyEditorSupport;

public class WorkerEditor extends PropertyEditorSupport {
	
	public void setAsText(String input) {
		String[] tokens=input.split(",");
		int length=tokens.length;
		int id= length>0 ? Integer.parseInt(tokens[0]):0;
		String name= length>1 ? tokens[1]:"null";
		int salary= length>2 ? Integer.parseInt(tokens[2]):0;
		Worker worker=new Worker();
		worker.setId(id);
		worker.setName(name);
		worker.setSalary(salary);
		setValue(worker);
	}
}
WorkerFactory,java
package study.property;

public class WorkerFactory {
	private Worker worker;

	public Worker getWorker() {
		return worker;
	}

	public void setWorker(Worker worker) {
		this.worker = worker;
	}
}
Worker.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="workerFactory" class="study.property.WorkerFactory">
	    <property name="worker" value="12,wahaha,20"/>
	</bean>
	<bean id="editorConfig" class="org.springframework.beans.factory.config.CustomEditorConfigurer" >  
        <property name="customEditors">  
        <!--CustomEditorConfigurer的属性customEditors是Map类型,
        Map的key表示遇到哪一个类型时要使用PropertyEditor,value表示对应要使用的PropertyEditor实现类  -->
            <map>  
                <entry key="study.property.Worker">  
                    <bean class="study.property.WorkerEditor"/>  
                </entry>  
            </map>
        </property>  
    </bean> 

</beans>

LearnPropertyEditor.java
package study.property;

import java.beans.PropertyEditorManager;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LearnPropertyEditor {
	public static void main(String[] args) {
		Worker worker=null;
		/**
		 * 第一种方式
		 */
		WorkerEditor editor=new WorkerEditor();
		editor.setAsText("10,llq,28");
		worker=(Worker)editor.getValue();
		System.out.println(worker);
		/**
		 * 第二种方式
		 */
		PropertyEditorManager.registerEditor(Worker.class, WorkerEditor.class);
		WorkerEditor editor2=(WorkerEditor) PropertyEditorManager.findEditor(Worker.class);
		editor2.setAsText("11,ppp,29");
		worker=(Worker)editor2.getValue();
		System.out.println(worker);
		/**
		 * 第三种方式
		 */
		ApplicationContext context=new ClassPathXmlApplicationContext("worker.xml");
		WorkerFactory factory=(WorkerFactory)context.getBean("workerFactory");
		worker=factory.getWorker();
		System.out.println(worker);
	}
}
运行结果:
worker[id:10,name:llq,salary:28]
worker[id:11,name:ppp,salary:29]
worker[id:12,name:wahaha,salary:20]







你可能感兴趣的:(java,spring,PropertyEditor)