1 16 17 package org.springframework.beans.support; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.net.URI ; 22 import java.net.URL ; 23 24 import org.springframework.beans.PropertyEditorRegistrar; 25 import org.springframework.beans.PropertyEditorRegistry; 26 import org.springframework.beans.propertyeditors.ClassEditor; 27 import org.springframework.beans.propertyeditors.FileEditor; 28 import org.springframework.beans.propertyeditors.InputStreamEditor; 29 import org.springframework.beans.propertyeditors.URIEditor; 30 import org.springframework.beans.propertyeditors.URLEditor; 31 import org.springframework.core.JdkVersion; 32 import org.springframework.core.io.Resource; 33 import org.springframework.core.io.ResourceEditor; 34 import org.springframework.core.io.ResourceLoader; 35 import org.springframework.core.io.support.ResourceArrayPropertyEditor; 36 import org.springframework.core.io.support.ResourcePatternResolver; 37 38 49 public class ResourceEditorRegistrar implements PropertyEditorRegistrar { 50 51 private final ResourceLoader resourceLoader; 52 53 54 61 public ResourceEditorRegistrar(ResourceLoader resourceLoader) { 62 this.resourceLoader = resourceLoader; 63 } 64 65 66 79 public void registerCustomEditors(PropertyEditorRegistry registry) { 80 ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader); 81 registry.registerCustomEditor(Resource.class, baseEditor); 82 registry.registerCustomEditor(InputStream .class, new InputStreamEditor(baseEditor)); 83 registry.registerCustomEditor(File .class, new FileEditor(baseEditor)); 84 registry.registerCustomEditor(URL .class, new URLEditor(baseEditor)); 85 86 ClassLoader classLoader = this.resourceLoader.getClassLoader(); 87 registry.registerCustomEditor(Class .class, new ClassEditor(classLoader)); 88 if (JdkVersion.isAtLeastJava14()) { 89 registry.registerCustomEditor(URI .class, new URIEditor(classLoader)); 90 } 91 92 if (this.resourceLoader instanceof ResourcePatternResolver) { 93 registry.registerCustomEditor(Resource[].class, 94 new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader)); 95 } 96 } 97 98 } 99 | Popular Tags |