1 16 17 package org.springframework.beans.factory.config; 18 19 import java.beans.PropertyEditor ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.springframework.beans.BeansException; 24 import org.springframework.beans.PropertyEditorRegistrar; 25 import org.springframework.beans.factory.BeanClassLoaderAware; 26 import org.springframework.core.Ordered; 27 import org.springframework.util.ClassUtils; 28 29 88 public class CustomEditorConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered { 89 90 private int order = Ordered.LOWEST_PRECEDENCE; 92 private PropertyEditorRegistrar[] propertyEditorRegistrars; 93 94 private Map customEditors; 95 96 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 97 98 99 public void setOrder(int order) { 100 this.order = order; 101 } 102 103 public int getOrder() { 104 return this.order; 105 } 106 107 117 public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) { 118 this.propertyEditorRegistrars = propertyEditorRegistrars; 119 } 120 121 128 public void setCustomEditors(Map customEditors) { 129 this.customEditors = customEditors; 130 } 131 132 public void setBeanClassLoader(ClassLoader beanClassLoader) { 133 this.beanClassLoader = beanClassLoader; 134 } 135 136 137 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 138 if (this.propertyEditorRegistrars != null) { 139 for (int i = 0; i < this.propertyEditorRegistrars.length; i++) { 140 beanFactory.addPropertyEditorRegistrar(this.propertyEditorRegistrars[i]); 141 } 142 } 143 144 if (this.customEditors != null) { 145 for (Iterator it = this.customEditors.entrySet().iterator(); it.hasNext();) { 146 Map.Entry entry = (Map.Entry ) it.next(); 147 Object key = entry.getKey(); 148 Class requiredType = null; 149 if (key instanceof Class ) { 150 requiredType = (Class ) key; 151 } 152 else if (key instanceof String ) { 153 String className = (String ) key; 154 requiredType = ClassUtils.resolveClassName(className, this.beanClassLoader); 155 } 156 else { 157 throw new IllegalArgumentException ( 158 "Invalid key [" + key + "] for custom editor: needs to be Class or String."); 159 } 160 Object value = entry.getValue(); 161 if (!(value instanceof PropertyEditor )) { 162 throw new IllegalArgumentException ("Mapped value [" + value + "] for custom editor key [" + 163 key + "] is not of required type [" + PropertyEditor .class.getName() + "]"); 164 } 165 beanFactory.registerCustomEditor(requiredType, (PropertyEditor ) value); 166 } 167 } 168 } 169 170 } 171 | Popular Tags |