下面列出了org.springframework.beans.PropertyEditorRegistry#registerCustomEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Registers all known
*
* @param registry
* @param locale
*/
public static void registerCustomEditors(PropertyEditorRegistry registry, Locale locale) {
// Formatters for the different number types.
NumberFormat floatFormat = NumberFormat.getInstance(locale);
NumberFormat integerFormat = NumberFormat.getIntegerInstance(locale);
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, locale);
registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
registry.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, floatFormat, true));
registry.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, floatFormat, true));
registry.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, floatFormat, true));
registry.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, floatFormat, true));
registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, floatFormat, true));
registry.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, floatFormat, true));
registry.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, integerFormat, true));
registry.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, integerFormat, true));
registry.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, integerFormat, true));
registry.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, integerFormat, true));
registry.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, integerFormat, true));
registry.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, integerFormat, true));
registry.registerCustomEditor(Date.class, new StructuredDateEditor(dateFormat, true));
registry.registerCustomEditor(Calendar.class, new StructuredDateEditor(dateFormat, true));
registerCustomEditors(registry);
}
/**
* Register the property editors provided by uimaFIT in the given property editor registry.
*
* @param aRegistry a property editor registry
*/
public static void registerUimaFITEditors(PropertyEditorRegistry aRegistry) {
aRegistry.registerCustomEditor(Charset.class, new CharsetEditor());
aRegistry.registerCustomEditor(Locale.class, new LocaleEditor());
aRegistry.registerCustomEditor(String.class, new GetAsTextStringEditor(aRegistry));
aRegistry.registerCustomEditor(LinkedList.class, new CustomCollectionEditor(LinkedList.class));
}
/**
* Override default editor, if possible (since that's what we really mean to do here);
* otherwise register as a custom editor.
*/
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
if (registry instanceof PropertyEditorRegistrySupport) {
((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
}
else {
registry.registerCustomEditor(requiredType, editor);
}
}
/**
* @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
*/
@Override
public void registerCustomEditors(PropertyEditorRegistry registry)
{
// add custom QName editor
registry.registerCustomEditor(QName.class, new QNameTypeEditor(namespaceService));
}
/**
* Initialize the given PropertyEditorRegistry with the custom editors
* that have been registered with this BeanFactory.
* <p>To be called for BeanWrappers that will create and populate bean
* instances, and for SimpleTypeConverter used for constructor argument
* and factory method type conversion.
* @param registry the PropertyEditorRegistry to initialize
*/
protected void registerCustomEditors(PropertyEditorRegistry registry) {
PropertyEditorRegistrySupport registrySupport =
(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
if (registrySupport != null) {
registrySupport.useConfigValueEditors();
}
if (!this.propertyEditorRegistrars.isEmpty()) {
for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
try {
registrar.registerCustomEditors(registry);
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
if (isCurrentlyInCreation(bce.getBeanName())) {
if (logger.isDebugEnabled()) {
logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
"] failed because it tried to obtain currently created bean '" +
ex.getBeanName() + "': " + ex.getMessage());
}
onSuppressedException(ex);
continue;
}
}
throw ex;
}
}
}
if (!this.customEditors.isEmpty()) {
for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
Class<?> requiredType = entry.getKey();
Class<? extends PropertyEditor> editorClass = entry.getValue();
registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
}
}
}
/**
* Override default editor, if possible (since that's what we really mean to do here);
* otherwise register as a custom editor.
*/
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
if (registry instanceof PropertyEditorRegistrySupport) {
((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
}
else {
registry.registerCustomEditor(requiredType, editor);
}
}
/**
* Gets a property editor given a specific bean and property path.
*
* @param bean The bean instance.
* @param path The property path.
* @return property editor
*/
public static PropertyEditor getPropertyEditor(Object bean, String path) {
Class<?> propertyType = getPrimitiveType(getPropertyType(bean, path));
PropertyEditor editor = null;
PropertyEditorRegistry registry = getPropertyEditorRegistry();
if (registry != null) {
editor = registry.findCustomEditor(propertyType, path);
if (editor != null && editor != registry.findCustomEditor(propertyType, null)) {
return editor;
}
if (registry instanceof BeanWrapper
&& bean == ((BeanWrapper) registry).getWrappedInstance()
&& (bean instanceof ViewModel)) {
ViewModel viewModel = (ViewModel) bean;
ViewPostMetadata viewPostMetadata = viewModel.getViewPostMetadata();
PropertyEditor editorFromView = viewPostMetadata == null ? null : viewPostMetadata.getFieldEditor(path);
if (editorFromView != null) {
registry.registerCustomEditor(propertyType, path, editorFromView);
editor = registry.findCustomEditor(propertyType, path);
}
}
}
if (editor != null) {
return editor;
}
return getPropertyEditor(propertyType);
}
/**
* Override default editor, if possible (since that's what we really mean to do here);
* otherwise register as a custom editor.
*/
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
if (registry instanceof PropertyEditorRegistrySupport) {
((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
}
else {
registry.registerCustomEditor(requiredType, editor);
}
}
/**
* Initialize the given PropertyEditorRegistry with the custom editors
* that have been registered with this BeanFactory.
* <p>To be called for BeanWrappers that will create and populate bean
* instances, and for SimpleTypeConverter used for constructor argument
* and factory method type conversion.
* @param registry the PropertyEditorRegistry to initialize
*/
protected void registerCustomEditors(PropertyEditorRegistry registry) {
PropertyEditorRegistrySupport registrySupport =
(registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
if (registrySupport != null) {
registrySupport.useConfigValueEditors();
}
if (!this.propertyEditorRegistrars.isEmpty()) {
for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
try {
registrar.registerCustomEditors(registry);
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
if (isCurrentlyInCreation(bce.getBeanName())) {
if (logger.isDebugEnabled()) {
logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() +
"] failed because it tried to obtain currently created bean '" +
ex.getBeanName() + "': " + ex.getMessage());
}
onSuppressedException(ex);
continue;
}
}
throw ex;
}
}
}
if (!this.customEditors.isEmpty()) {
for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
Class<?> requiredType = entry.getKey();
Class<? extends PropertyEditor> editorClass = entry.getValue();
registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
}
}
}
/**
* Override default editor, if possible (since that's what we really mean to do here);
* otherwise register as a custom editor.
*/
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
if (registry instanceof PropertyEditorRegistrySupport) {
((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
}
else {
registry.registerCustomEditor(requiredType, editor);
}
}
/**
* {@inheritDoc}
*/
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Byte.class, new HalfwidthNumberEditor(Byte.class, allowEmpty, message));
registry.registerCustomEditor(Short.class, new HalfwidthNumberEditor(Short.class, allowEmpty, message));
registry.registerCustomEditor(Integer.class, new HalfwidthNumberEditor(Integer.class, allowEmpty, message));
registry.registerCustomEditor(Long.class, new HalfwidthNumberEditor(Long.class, allowEmpty, message));
registry.registerCustomEditor(BigInteger.class, new HalfwidthNumberEditor(BigInteger.class, allowEmpty, message));
registry.registerCustomEditor(BigDecimal.class, new HalfwidthDecimalEditor(BigDecimal.class, allowEmpty, message));
}
/**
* {@inheritDoc}
*/
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new DateEditor(allowEmpty, true, pattern, message));
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
// 1. 通用类型转换
// 2. Java Bean 属性类型转换
registry.registerCustomEditor(User.class, "context", new StringToPropertiesPropertyEditor());
}
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(InetSocketAddress.class, this);
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true));
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(InetSocketAddress.class, this);
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
// 为 LocalDate 类型属性注册 LocalDateEditor
registry.registerCustomEditor(LocalDate.class, new LocalDateEditor());
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"), true));
}
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Set.class, new ListEditor());
}