提交 6d4faa6c 编写于 作者: J Juergen Hoeller

ApplicationContext registers context-specific ClassArrayEditor for its bean ClassLoader (SPR-1461)

上级 d8ecc76d
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -19,6 +19,7 @@ package org.springframework.beans.propertyeditors;
import java.beans.PropertyEditorSupport;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
......@@ -30,6 +31,7 @@ import org.springframework.util.StringUtils;
* to the standard {@link Class#forName(String)} method.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @since 2.0
*/
public class ClassArrayEditor extends PropertyEditorSupport {
......@@ -52,8 +54,7 @@ public class ClassArrayEditor extends PropertyEditorSupport {
* (or pass <code>null</code> for the thread context <code>ClassLoader</code>)
*/
public ClassArrayEditor(ClassLoader classLoader) {
this.classLoader = classLoader != null
? classLoader : ClassUtils.getDefaultClassLoader();
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
......@@ -76,14 +77,9 @@ public class ClassArrayEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
Class[] classes = (Class[]) getValue();
if (classes == null || classes.length == 0) {
if (ObjectUtils.isEmpty(classes)) {
return "";
}
return toCommaDelimitedString(classes);
}
private static String toCommaDelimitedString(Class[] classes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < classes.length; ++i) {
if (i > 0) {
......
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -53,8 +53,7 @@ public class ClassEditor extends PropertyEditorSupport {
* (or <code>null</code> for the thread context ClassLoader)
*/
public ClassEditor(ClassLoader classLoader) {
this.classLoader =
(classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
}
......
......@@ -27,6 +27,7 @@ import org.xml.sax.InputSource;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.PropertyEditorRegistrySupport;
import org.springframework.beans.propertyeditors.ClassArrayEditor;
import org.springframework.beans.propertyeditors.ClassEditor;
import org.springframework.beans.propertyeditors.FileEditor;
import org.springframework.beans.propertyeditors.InputSourceEditor;
......@@ -89,8 +90,9 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
ClassLoader classLoader = this.resourceLoader.getClassLoader();
doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));
if (this.resourceLoader instanceof ResourcePatternResolver) {
doRegisterEditor(registry, Resource[].class,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册