From 7591406f2e6d854b248dc4d978219613a2b940c7 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sat, 28 Feb 2009 16:37:02 +0000 Subject: [PATCH] Renamed 'prototypeBeanName' to 'targetBeanName' and added eager validation of its non-singleton scope. --- .../ServletRequestBindingTransformer.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/transformer/ServletRequestBindingTransformer.java b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/transformer/ServletRequestBindingTransformer.java index 48bdc9a899..976cb0931f 100644 --- a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/transformer/ServletRequestBindingTransformer.java +++ b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/transformer/ServletRequestBindingTransformer.java @@ -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 extends AbstractPayloadTransformer implements BeanFactoryAware { +public class ServletRequestBindingTransformer extends AbstractPayloadTransformer + implements BeanFactoryAware, InitializingBean { private final Class targetType; - private volatile String prototypeBeanName; + private volatile String targetBeanName; private volatile WebBindingInitializer webBindingInitializer; private volatile BeanFactory beanFactory; + private volatile boolean validated; + public ServletRequestBindingTransformer(Class targetType) { Assert.notNull(targetType, "targetType must not be null"); @@ -55,8 +59,15 @@ public class ServletRequestBindingTransformer 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 not be a singleton, and it must be + * compatible with the {@link #targetType}. + *

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 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 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(); } }