SpringMvc @InitBinder


這篇博客記錄@InitBinder怎么起作用、起什么作用?

  首先,該注解被解析的時機,是該匹配Controller的請求執行映射的方法之前; 同時 @InitBinder標注的方法執行是多次的,一次請求來就執行一次。

  當某個Controller上的第一次請求由SpringMvc前端控制器匹配到該Controller之后,根據Controller的 class 類型 查找 所有方法上標注了@InitBinder的方法,並且存入RequestMappingHandlerAdapter的 initBinderCache,下次一請求執行對應業務方法之前時,可以走initBinderCache緩存,而不用再去解析@InitBinder; 所以 initBinder是controller級別的,一個controller實例中的所有@initBinder 只對該controller有效;

 

功能一.注冊Controller級別的 MVC屬性編輯器 (屬性編輯器功能就是將Web請求中的屬性轉成我們需要的類型) 

  @InitBinder唯一的一個屬性value,作用是限制對哪些 @RequestMapping 方法起作用,具體篩選條件就是通過@RequestMapping方法入參來篩選,默認不寫就代表對所有@RequestMapping的方法起作用;

  @InitBinder標注的方法, 方法入參和 @RequestMapping方法入參可選范圍一樣(這里指的是比如HttpServletRequestModelMap這些), 通常一個入參 WebDataBinder 就夠我們使用了; @InitBinder標注的方法返回值, 必須為null,這里我理解的是運行期的返回值;如果運行時返回值不為null,拋出異常 “@InitBinder methods should return void:”,編譯時IDEA會提示@InitBinder應該返回null,但是不影響編譯通過;

@InitBinder
    public  void initBinder(WebDataBinder binder, HttpServletRequest request){
        System.out.println(request.getParameter("date"));
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("MM-dd-yyyy"),false));
    }

  上面是一個@InitBinder的簡單用法, 其中binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("MM-dd-yyyy"),false)); 這樣一句話,作用就是將 自定義的MVC屬性編輯器PropertyEditor 注冊到當前binder的typeConverter的customEditors集合中,每一次請求和后端交互,每一個Controller方法入參都會創建一個Binder對象,binder對象相當於來完成請求和后端之間參數類型轉換的職能類;  注意,每次請求都會創建新的binder對象,就是說上次請求的customEditors不可復用 , 每次執行都會添加到當前方法入參交互的binder的customEditors中,而且每次執行真正請求方法之前,會把 匹配上的@InitBinder標注的方法執行一遍才開始處理;

  當請求中參數和方法入參開始進行轉換的時候,都會先使用自定義注冊的PropertyEditor,會首先根據需要的類型去binder的typeConverter的typeConverterDelegate的propertyEditorRegistry的cutomEditors集合中查找,有點繞,先記錄下,typeConverterDelegate的propertyEditorRegistry就是typeConverter對象本身, 所以就是去typeConverter對象的cutomEditors尋找自定義注冊的屬性編輯器,又回到了原點。 比如去customEditors中根據key為Date.class查找editor,  分為兩種情況,一種是找到了合適的屬性編輯器,調用其setValue、setAsText方法, 之后使用getValue就得到轉換后的值,  得到了轉換后的值,可能不是我們想要的類型,這時候就會使用 conversionService 重新來過,放棄之前的轉換;  是我們想要的類型就直接返回轉換后的值;  第二種情況是沒找到合適的屬性編輯器, 直接調用 ConversionService 進行轉換工作;

上面方式是@InitBinder作為Controller級別的 SpringMvc屬性編輯器,  下面記錄一下全局級別(所有@Controller)的屬性編輯器;

Xml中 注冊全局屬性編輯器到 ConfigurableWebBindingInitializer上,再將其注冊到 RequestMappingHandlerAdapter里;記錄原因,綁定binder的屬性編輯器時候,會將當前的 

Initializer中的屬性編輯器也給注冊到binder中,這樣就能實現全局的屬性編輯器

<!--<mvc:annotation-driven/>-->
    <!--取消注解驅動的話Spring4.3就要手動注冊RequestMappingHandlerMapping、RequestMappingHandlerAdapter-->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">

</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer" ref="initializer1"/>
</bean>

<bean id="initializer1" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    <property name="propertyEditorRegistrars">
       <list>
            <bean class="demo2.MyPropertyEditor"/>
       </list>
    </property>
</bean>

MyPropertyEditor.java

public class MyPropertyEditor implements PropertyEditorRegistrar {

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

    public static class MyDateEditor extends PropertyEditorSupport {
        @Override
        public void setValue(Object value) {
            super.setValue(value);
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            Date d=null;
            try {
                System.out.println("我調用自己的全局MVC屬性編輯器");
                d=new SimpleDateFormat("MM-dd-yyyy").parse(text);
                setValue(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

上面兩段代碼就可以注冊 自定義的屬性編輯器到 所有@Controller中,相當於之前每個 Controller都使用了 @InitBinder;  但是這樣寫不太友好,SpringMvc<mvc:annotation-driven/>替我們注冊的很多東西可能就沒法使用了,意義不大,所以簡單改造了一下: 在之前記錄的Spring加載初始化容器的流程基礎上改造了下;

 

image

spring  XML文件仍然使用注解驅動:

<mvc:annotation-driven/>
<bean id="globalBeanDefinitionRegistry" class="demo2.GlobalBeanDefinitionRegistry">
    <property name="editorRegistrars">
        <list>
            <bean class="demo2.MyPropertyEditor"/>
        </list>
    </property>
</bean>

 

自定義的 GlobalBeanDefinitionRegistry代碼如下:  簡單說下原理,在Spring原有注解驅動的基礎上,改變了webBindingInitializer,使它可以自由地配置方式添加屬性編輯器;優點就是,不破壞SpringMvc注解驅動帶給我們的好處,可以自定義添加屬性全局的編輯器;缺點就是 代碼中判斷邏輯的 處理器映射器適配器 RequestMappingHandlerAdapter是硬編碼的,Spring4可能還好用,Spring3突然又不支持了,同樣也是有解決方案的

用法其實就是在Spring初始化容器中對象之前移花接木地替換我們的 webBindingInitalizer.

public class GlobalBeanDefinitionRegistry  implements BeanDefinitionRegistryPostProcessor {
        private PropertyEditorRegistrar[]  editorRegistrars;
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        if(registry.containsBeanDefinition("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter")){
            BeanDefinition beanDefinition = registry.getBeanDefinition("org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter");
            PropertyValue pv = beanDefinition.getPropertyValues().getPropertyValue("webBindingInitializer");
            BeanDefinition intializer= (BeanDefinition) pv.getValue();
            intializer.getPropertyValues().addPropertyValue("propertyEditorRegistrars",editorRegistrars);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
    public void setEditorRegistrars(PropertyEditorRegistrar[] editorRegistrars) {
        this.editorRegistrars = editorRegistrars;
    }
}

因為@InitBinder方法 作為Controller級別的屬性編輯器 和全局的自定義Mvc屬性編輯器沒有太大差別,所以下面講一些別的用法:

 

功能二. WebDatabinder的setFieldDefaultPrefix(String fieldDefaultPrefix)

作用:將SpringMvc請求參數帶有fieldDefaultPrefix的參數,去掉該前綴再綁定到對應請求入參上

  想來想去,也沒搞明白這個方法的意義,以及實際用途,想到一種實際中可能出現的情況,覺得是個有幾率出現的事情,正好可以用該方法可以解決;

問題:假設 后台 用兩個對象來接受請求參數(SpringMvc可以做到),Pojo、Pojo2對象,他們兩個屬性如下:發現兩個對象都有個name屬性,問題來了,前台我們不能傳兩個 name屬性吧,那樣接收肯定會出錯(我這里沒嘗試過),原有對象不修改的基礎上可行方案如下:

@Setter
@Getter
@ToString  // 代碼整潔所以使用lombok,可以自行百度
public class Pojo {
    private String name;
    private String haircolor;
}

@Setter
@ToString
public class Pojo2 {
    private String name;
    private int age;
}

 

在兩個對象上各使用@ModelAttribute,來給對象分別起別名, @Initbinder這里value屬性指定 別名,然后給不同的參數加上了前綴 person. 、cat.  ;注意這里的兩個 . 號 

 @RequestMapping("/test3")
    @ResponseBody
    public String test3(@ModelAttribute("person") Pojo person, @ModelAttribute("cat") Pojo2 cat){
        return "test Response Ok!"+person+","+cat;
    }

    @InitBinder("person")
    public void initPerson(WebDataBinder binder){
          binder.setFieldDefaultPrefix("person.");
    }

    @InitBinder("cat")
    public void initCat(WebDataBinder binder){
        binder.setFieldDefaultPrefix("cat.");
    }

 

這樣請求URL:………… test3?person.name=lvbinbin&cat.name=xiaobinggan&haircolor=black&age=20 這里前台改動的地方就是 person.name和 cat.name,而其他獨有屬性不需要前綴也可以對應賦給pojo、pojo2;  補充說明,@ModelAttribute注解不可以省略,通過這個取的別名來決定哪個@InitBinder對其生效

查看效果圖:

image

 

簡單記錄下,因為這個 defaultPrefix 所在代碼確實不好找:

protected void doBind(MutablePropertyValues mpvs) {
		checkFieldDefaults(mpvs);    //這里就是 defaultPrefix生效的地方
		checkFieldMarkers(mpvs);
		super.doBind(mpvs);
	}
//效果就是請求中包含defaultPrefix的,將其前綴去掉保存
protected void checkFieldDefaults(MutablePropertyValues mpvs) {
	if (getFieldDefaultPrefix() != null) {
		String fieldDefaultPrefix = getFieldDefaultPrefix();
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldDefaultPrefix)) {
				String field = pv.getName().substring(fieldDefaultPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					mpvs.add(field, pv.getValue());
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}

 

功能三.WebDataBinder的setDisallowedFields(String ….disallowedFields);

作用:SpringMvc接收請求參數時候,有些參數禁止的,不想接收,我也沒遇到過啥情況禁止接收參數,這時候可以設置setDisallowedFields不接受參數

 @RequestMapping("/test4")
    @ResponseBody
    public String test4(@ModelAttribute("pojo2") Pojo2 pojo){
        return "test Response Ok!"+pojo;
    }

    @InitBinder("pojo2")
    public void disallowFlied(WebDataBinder binder){
        binder.setDisallowedFields("age");
    }

 

簡單貼下效果圖:

image

 

正好發現了日志輸出證明這一點:

image

那就順便把代碼貼一下,萬一要用呢;  另外補充一下,disallowedFields支持 * 通配符;

protected void checkAllowedFields(MutablePropertyValues mpvs) {
		PropertyValue[] pvs = mpvs.getPropertyValues();
		for (PropertyValue pv : pvs) {
			String field = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
			if (!isAllowed(field)) {
				mpvs.removePropertyValue(pv);
				getBindingResult().recordSuppressedField(field);
				if (logger.isDebugEnabled()) {
					logger.debug("Field [" + field + "] has been removed from PropertyValues " +
							"and will not be bound, because it has not been found in the list of allowed fields");
				}
			}
		}
	}
protected boolean isAllowed(String field) {
	String[] allowed = getAllowedFields();
	String[] disallowed = getDisallowedFields();
	return ((ObjectUtils.isEmpty(allowed) || PatternMatchUtils.simpleMatch(allowed, field)) &&
			(ObjectUtils.isEmpty(disallowed) || !PatternMatchUtils.simpleMatch(disallowed, field)));
}
 
        

 

@Initbinder的用法簡而言之,就是Controller級別的屬性編輯器,將請求中的String類型轉為我們需要的參數,但是從效率、內存分析,感覺一直在創建新的屬性編輯器集合,如果屬性編輯器太多是不是會占用大量內存呢,那請求達到一定多的數量,這個對象是不是太多了呢?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM