java.beans.PropertyEditorManager.findEditor(Locale.class) 在 spring 中返回 null

标签 java spring

根据spring ref docs :

Spring uses the java.beans.PropertyEditorManager to set the search path for property editors that might be needed.

但我在这里得到空:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.util.Locale;

public class DefaultEditorsTest {

    public static void main (String[] args) {
        ApplicationContext context = new
                            ClassPathXmlApplicationContext("spring-config.xml");

        PropertyEditor editor = PropertyEditorManager.findEditor(Locale.class);
        System.out.println(editor);
    }
}

输出:

Sep 13, 2016 8:47:29 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17f052a3: startup date [Tue Sep 13 20:47:29 CDT 2016]; root of context hierarchy
Sep 13, 2016 8:47:29 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
null 

我的 spring-config.xml 实际上注册了几个 bean。为什么我的会变成null?如果这不是正确的方法那么如何获取 spring 的 LocaleEditor 和其他实例?我想在 swing 应用程序中使用它,并且我可能会使用 spring 提供的许多应用程序,所以我不想手动注册它们。 提前致谢。

最佳答案

您可以使用BeanWrapperImpl。( http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/BeanWrapperImpl.html )

Note: Auto-registers default property editors from the org.springframework.beans.propertyeditors package, which apply in addition to the JDK's standard PropertyEditors.

BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);  
PropertyEditor editor = wrapper.getDefaultEditor(Locale.class);
System.out.println(editor);

关于java.beans.PropertyEditorManager.findEditor(Locale.class) 在 spring 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481361/

相关文章:

java - Spring Security - Autowiring 不工作

spring - 使用 spring xml 作为后台进程/线程启动 jetty

java - 如何嵌套 Spring JMS MessageConverters

java - 尝试在模态对话框中更新 JLabel

java - 如何使用java将RSA私钥导出为pem格式

java - 将两张不同类型的 map 合并为一张 map

java - 无法使用 Iterator 迭代 List,但在使用 for every 时工作正常

java - 带注释 Controller 的 SpringMVC : requested resource is not available

java - 使用套接字的服务器和客户端

java - Struts 2中的ActionContext对于当前请求来说是唯一的吗?