Spring PropertyEditor自定义属性编辑器

Spring 使用PropertyEditors的接口来实现对象和字符串之间的转换,比如将 2007-14-09转化为日期类型等,可以通过注册自定义编辑器来实现此功能

Spring使用了属性编辑的例子

1.在bean上设置属性是使用PropertyEditors完成的,当遇到java.lang.String作为XML文件中声明某个bean的属性值时,Spring将使用ClassEditor尝试将参数解析为类对象。

2.Spring MVC框架使用多种PropertyEditors分析HTTP请求的各种参数,开发者可以在CommandController的所有子类中手动绑定。

3.Spring内置了许多propertyEditors用于简化处理,这些PE都位于propertyeditors包中,大多是都是由BeanWrapperImpl注册,当属性编辑器以某种方式进行配置时,开发者仍可以注册自定义的变体用于覆盖默认的变量

内置的PropertyEditors


标准的JavaBean可以自动发现PropertyEditor类(无需显示注册)前提是此类与需处理的类位于同一个包中,并且处理类的类名需要遵循如下的规则:被处理类名+Editor   例如  : Foo  FooEditor

例子:将字符串 转化为ExoticType对象 采用在同一个包下的方式

1.定义测试类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExoticType {

    private String name;
}
@Data
public class DependsOnExoticType {

    private ExoticType type;
}

2.编写配置

my:
  birthday: 1993-09-25 19:15:10
  value: aNameForExoticType
3.注册对象
@Value("${my.value}")
private ExoticType exoticType;
@Bean
    public DependsOnExoticType dependsOnExoticType() {
        DependsOnExoticType type = new DependsOnExoticType();
        type.setType(exoticType);
        return type;
    }

4.编写类型转化类 注意此类需要与ExoticType在同一个包下

public class ExoticTypeEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) {
        setValue(new ExoticType(text.toUpperCase()));
    }

}
5.测试:
    @Autowired
    private DependsOnExoticType dependsOnExoticType;

    @GetMapping(value = "/get2")
    public Object get2() {
        return dependsOnExoticType;
//        BeanFactory factory = new ClassPathXmlApplicationContext("Text.xml");
//        return factory.getBean("sample", DependsOnExoticType.class);
//        return "null";
    }
可以看到输出为 
{
  "type": {
    "name": "ANAMEFOREXOTICTYPE"
  }
}

注意点就是:如果不编写属性转换处理类,也就是去掉第4步

输出为

{
  "type": {
    "name": "aNameForExoticType"
  }
}

似乎是采用配置值,调用了以String类型的构造函数得到的结果。


例子:采用注册CustomEditorConfigurer的形式(此形式采用XML的配置有效,但是使用@Bean注入的形式没有生效,可能和@Value有关,有待查证)

测试类不变,编写如下 Text.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="sample" class="com.test.entity.DependsOnExoticType">
        <property name="type" value="sfdsdfgdfgfgsfgsfdg"/>
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="com.test.entity.ExoticType" value="com.test.controller.ExoticTypeEditor"/>
            </map>
        </property>
    </bean>
</beans>

也就是想对象以及CustomEditorConfigurer对象注入到容器

    @GetMapping(value = "/get2")
    public Object get2() {
        BeanFactory factory = new ClassPathXmlApplicationContext("Text.xml");
        return factory.getBean("sample", DependsOnExoticType.class);
    }

输出为:

{
  "type": {
    "name": "SFDSDFGDFGFGSFGSFDG"
  }
}

例子:使用PropertyEditorRegistors(使用@Bean注入仍旧没有效果,需要查证,可能是注入的方式不对)

测试类不变

编写CustomPropertyEditorRegistrar 

public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
    @Override
    public void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
        propertyEditorRegistry.registerCustomEditor(ExoticType.class, new ExoticTypeEditor());
    }
}

xml做如下变更

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="sample" class="com.test.entity.DependsOnExoticType">
        <property name="type" value="sfdsdfgdfgfgsfgsfdg"/>
    </bean>

    <!--<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="com.test.entity.ExoticType" value="com.test.controller.ExoticTypeEditor"/>
            </map>
        </property>
    </bean>-->

    <bean id="customPropertyEditorRegistrar" class="com.test.entity.CustomPropertyEditorRegistrar"></bean>


    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="propertyEditorRegistrars">
            <list>
                <ref bean="customPropertyEditorRegistrar"/>
            </list>
        </property>
    </bean>
</beans>

编写测试类

    @GetMapping(value = "/get2")
    public Object get2() {
        BeanFactory factory = new ClassPathXmlApplicationContext("Text.xml");
        return factory.getBean("sample", DependsOnExoticType.class);
    }

输出

{
  "type": {
    "name": "SFDSDFGDFGFGSFGSFDG"
  }
}

最后例子:PropertyEditor在Spring MVC中的应用

1.编写测试类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExoticType {

    private String name;
}

2.编写属性转换实现类

public class ExoticTypeEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) {
        setValue(new ExoticType(text.toUpperCase()));
    }

}

3.在Controller中使用

    @InitBinder
    public void initBinder( ServletRequestDataBinder binder){
        binder.registerCustomEditor(ExoticType.class,new ExoticTypeEditor());

    }

    @GetMapping(value = "/get3")
    public Object get(ExoticType exoticType) {
        return exoticType;
    }

4.输出如下:

127.0.0.1:8886/get3?type=dfdfgdsfgsd

{
  "name": "DFDFGDSFGSD"
}

5.在controller或者如下使用

   @GetMapping(value = "/get3")
    public Object get(DependsOnExoticType dependsOnExoticType) {
        return dependsOnExoticType;
    }

6.输出如下

127.0.0.1:8886/get3?type=dfdfgdsfgsd&name=123

{
  "type": {
    "name": "DFDFGDSFGSD"
  },
  "name": "123"
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值