[maven + spring]SpringInAction 注册自定义属性编辑器时出现问题

引用

[maven + spring]SpringInAction 注册自定义属性编辑器时出现问题

功能描述:做一个电话号码自定义属性编辑器(摘自Spirng In Action例子)
**********************************************************************
maven的pom.xml中得加入如下代码
	<dependency>
	  <groupId>cglib</groupId>
	  <artifactId>cglib</artifactId>
	  <version>3.1</version>
	</dependency>

**********************************************************************

1.sec3.xml中
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
	<property name="customEditors">
		<map>
			<entry key="com.lh.springtest02.section3.PhoneNumber">
				<bean id="phoneEditor" class="com.lh.springtest02.section3.PhoneEditor"/>
			</entry>
		</map>
	</property>
</bean>
	
<bean id="contact" class="com.lh.springtest02.section3.Contact">
	<property name="phoneNumber" value="888-555-1212"></property>
</bean>

2.PhoneNumber..java
public class PhoneNumber {
	private String areaCode;
	private String prefix;
	private String number;
	
	public PhoneNumber() {
	}
	
	public PhoneNumber(String areaCode, String prefix, String number) {
		this.areaCode = areaCode;
		this.prefix = prefix;
		this.number = number;
	}
	
	public String toString() {
		return areaCode + prefix + number;
	}
	
}

3.PhoneEditor.java
import java.beans.PropertyEditorSupport;
public class PhoneEditor extends PropertyEditorSupport {
	public void setAsText(String textValue) {
		String stripped = stripNunNumeric(textValue);
		String areaCode = stripped.substring(0, 3);
		String prefix = stripped.substring(3, 6);
		String number = stripped.substring(6);
		PhoneNumber ph = new PhoneNumber(areaCode, prefix, number);
		setValue(ph);
	}

	private String stripNunNumeric(String original) {
		// TODO Auto-generated method stub
		StringBuffer allNumeric = new StringBuffer();
		for(int i = 0; i < original.length(); i++) {
			char c = original.charAt(i);
			if (Character.isDigit(c)) {
				allNumeric.append(c);
			}
		}
		return allNumeric.toString();
	}
}

4. Contact.java
public class Contact {
	private PhoneNumber phoneNumber;

	public void setPhoneNumber(PhoneNumber phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public void seePhoneNumber() {
		System.out.println(phoneNumber);
	}
	
}


你可能感兴趣的:(spring,maven,xml,自定义属性编辑器)