Renamed 'prototypeBeanName' to 'targetBeanName' and added eager validation of its non-singleton scope.
This commit is contained in:
@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.integration.transformer.AbstractPayloadTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.bind.ServletRequestDataBinder;
|
||||
@@ -32,22 +33,25 @@ import org.springframework.web.servlet.handler.DispatcherServletWebRequest;
|
||||
/**
|
||||
* Message Transformer that expects a {@link HttpServletRequest} as input and binds
|
||||
* its parameter map to a target instance. The target instance may be a non-singleton
|
||||
* bean as specified by the {@link #prototypeBeanName} property. Otherwise, this
|
||||
* transformer's {@link #type} must provide a default no-arg constructor.
|
||||
* bean as specified by the {@link #setTargetBeanName(String) 'targetBeanName'} property.
|
||||
* Otherwise, this transformer's target type must provide a default no-arg constructor.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransformer<HttpServletRequest, T> implements BeanFactoryAware {
|
||||
public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransformer<HttpServletRequest, T>
|
||||
implements BeanFactoryAware, InitializingBean {
|
||||
|
||||
private final Class<T> targetType;
|
||||
|
||||
private volatile String prototypeBeanName;
|
||||
private volatile String targetBeanName;
|
||||
|
||||
private volatile WebBindingInitializer webBindingInitializer;
|
||||
|
||||
private volatile BeanFactory beanFactory;
|
||||
|
||||
private volatile boolean validated;
|
||||
|
||||
|
||||
public ServletRequestBindingTransformer(Class<T> targetType) {
|
||||
Assert.notNull(targetType, "targetType must not be null");
|
||||
@@ -55,8 +59,15 @@ public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransfor
|
||||
}
|
||||
|
||||
|
||||
public void setPrototypeBeanName(String prototypeBeanName) {
|
||||
this.prototypeBeanName = prototypeBeanName;
|
||||
/**
|
||||
* Specify the name of a bean definition to use when creating the target
|
||||
* instance. The bean must <em>not</em> be a singleton, and it must be
|
||||
* compatible with the {@link #targetType}.
|
||||
* <p>If no 'targetBeanName' value is provided, the target type must
|
||||
* provide a default, no-arg constructor.
|
||||
*/
|
||||
public void setTargetBeanName(String targetBeanName) {
|
||||
this.targetBeanName = targetBeanName;
|
||||
}
|
||||
|
||||
public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer) {
|
||||
@@ -67,6 +78,20 @@ public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransfor
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() {
|
||||
this.validateTargetBeanIfNecessary();
|
||||
}
|
||||
|
||||
private void validateTargetBeanIfNecessary() {
|
||||
if (this.targetBeanName != null && !this.validated) {
|
||||
Assert.notNull(this.beanFactory, "beanFactory is required for binding to a bean");
|
||||
if (this.beanFactory.isSingleton(this.targetBeanName)) {
|
||||
throw new IllegalArgumentException("binding target bean must not be a singleton");
|
||||
}
|
||||
this.validated = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T transformPayload(HttpServletRequest request) throws Exception {
|
||||
@@ -86,14 +111,11 @@ public class ServletRequestBindingTransformer<T> extends AbstractPayloadTransfor
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object getTarget() throws InstantiationException, IllegalAccessException {
|
||||
if (this.prototypeBeanName == null) {
|
||||
return this.targetType.newInstance();
|
||||
if (this.targetBeanName != null) {
|
||||
this.validateTargetBeanIfNecessary();
|
||||
return (T) this.beanFactory.getBean(this.targetBeanName, this.targetType);
|
||||
}
|
||||
Assert.notNull(this.beanFactory, "beanFactory is required for binding to a prototype bean");
|
||||
if (this.beanFactory.isSingleton(this.prototypeBeanName)) {
|
||||
throw new IllegalArgumentException("binding target bean must not be a singleton");
|
||||
}
|
||||
return (T) this.beanFactory.getBean(this.prototypeBeanName, this.targetType);
|
||||
return this.targetType.newInstance();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user