Spring自定义属性解析,PropertyEditor的使用

PropertyEditor的使用

PropertyEditor是spring提供的一种对属性自定义解析的一种方式,如:用户传入时间戳,直接将其转换为LocalDate

spring中的使用

1. 定义自定义PropertyEditor

这里使用的PropertyEditorSupport是spring提供的PropertyEditor的实现,直接继承使用,主要重写setAsText方法对属性的转换

package com.armin.self_editor;

import java.beans.PropertyEditorSupport;
import java.time.LocalDate;

public class LocalDatePropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Long timeMillis = null;
        try {
            timeMillis = Long.valueOf(text);
        } catch (NumberFormatException e) {
          	//todo 转换异常的处理
        }
     		LocalDate localDate = Instant.ofEpochMilli(timeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        this.setValue(localDate);
    }
}

2. 将其注册到自定义的编辑器中

这里需要实现PropertyEditorRegistrar并重写registerCustomEditors方法

package com.armin.self_editor;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import java.time.LocalDate;

public class LocalDatePropertyEditorRegistrar implements PropertyEditorRegistrar {

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
    }
}

3. 编写转换的demo

package com.armin.self_editor;

import java.time.LocalDate;

@Data
public class DateCustomer {
    private LocalDate date;
}

4. 配置xml文件

注入解析的bean并将自定义属性编辑器注册到CustomEditorConfigurer


<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="customer" class="com.armin.self_editor.DateCustomer">
          <property name="date" value="1694534400000"/>
      bean>
  		
      
  		<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    			<property name="propertyEditorRegistrars">
  						<list>-->
  		            <bean class="com.armin.self_editor.LocalDatePropertyEditorRegistrar"/>
  			      list>
         	property>
  		bean>

      
      
      









beans>

4. 测试类

package com.armin.self_editor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SelfEditorTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("armin-self-editor.xml");
        DateCustomer customer = ac.getBean(DateCustomer.class);
        System.out.println(customer);
    }

}

springboot中的使用

如果想要在Controller层对web传入的参数进行自定义转换处理需要实现WebBindingInitializer接口,在initBinder方法中使用传入的WebDataBinder参数调用registerCustomEditor传入我们需要转换的类型与自定义转换处理的PropertyEditor对象

package com.armin.self_editor;

import com.armin.self_editor.LocalDatePropertyEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;

import java.time.LocalDate;

public class MyWebBindingInitializer implements WebBindingInitializer {
  	@Override
  	public void initBinder(WebDataBinder webDataBinder) {
      	webDataBinder.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
    }
}

然后将该对象设置到RequestMappingHandlerAdapter中的webBindingInitializer属性中

package com.armin.self_editor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.Resource;

@Configuration
public class WebConfiguration {
  	@Resource
  	public void setWebBindingInitializer (RequestMappingHanlderAdapter adapter) {
      	adapter.setWebBindingInitializer(new MyWebBindingInitializer());
    }
}

至此已完成了Controller层传参的全局处理(时间戳转换为LocalDate的处理)

同时如果需要对单个Controller中使用该转换可以在需要转换的Controller加入如下代码

@InitBinder
public void initBinder(WebDataBinder binder) {
  	binder.registerCustomEditor(LocalDate.class, new LocalDatePropertyEditor());
}

WebDataBinder实际上就是实现了PropertyEditorRegistrySpringBoot中的一种封装方式

你可能感兴趣的:(Spring,spring,java,后端)