001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.bind.support; 018 019import org.springframework.web.bind.WebDataBinder; 020import org.springframework.web.context.request.NativeWebRequest; 021 022/** 023 * Create a {@link WebRequestDataBinder} instance and initialize it with a 024 * {@link WebBindingInitializer}. 025 * 026 * @author Rossen Stoyanchev 027 * @since 3.1 028 */ 029public class DefaultDataBinderFactory implements WebDataBinderFactory { 030 031 private final WebBindingInitializer initializer; 032 033 034 /** 035 * Create a new {@code DefaultDataBinderFactory} instance. 036 * @param initializer for global data binder initialization 037 * (or {@code null} if none) 038 */ 039 public DefaultDataBinderFactory(WebBindingInitializer initializer) { 040 this.initializer = initializer; 041 } 042 043 044 /** 045 * Create a new {@link WebDataBinder} for the given target object and 046 * initialize it through a {@link WebBindingInitializer}. 047 * @throws Exception in case of invalid state or arguments 048 */ 049 @Override 050 public final WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName) 051 throws Exception { 052 053 WebDataBinder dataBinder = createBinderInstance(target, objectName, webRequest); 054 if (this.initializer != null) { 055 this.initializer.initBinder(dataBinder, webRequest); 056 } 057 initBinder(dataBinder, webRequest); 058 return dataBinder; 059 } 060 061 /** 062 * Extension point to create the WebDataBinder instance. 063 * By default this is {@code WebRequestDataBinder}. 064 * @param target the binding target or {@code null} for type conversion only 065 * @param objectName the binding target object name 066 * @param webRequest the current request 067 * @throws Exception in case of invalid state or arguments 068 */ 069 protected WebDataBinder createBinderInstance(Object target, String objectName, NativeWebRequest webRequest) 070 throws Exception { 071 072 return new WebRequestDataBinder(target, objectName); 073 } 074 075 /** 076 * Extension point to further initialize the created data binder instance 077 * (e.g. with {@code @InitBinder} methods) after "global" initializaton 078 * via {@link WebBindingInitializer}. 079 * @param dataBinder the data binder instance to customize 080 * @param webRequest the current request 081 * @throws Exception if initialization fails 082 */ 083 protected void initBinder(WebDataBinder dataBinder, NativeWebRequest webRequest) throws Exception { 084 } 085 086}