Announcement Announcement Module
Collapse
No announcement yet.
Type converters registered for a parameter type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Type converters registered for a parameter type

    Hi,

    I'm using Spring 2.5.6

    I have a class which is used as a wrapper for values of different types. This wrapper is often returned from methods or other properties during Spring configuration and during runtime. In particular, it is often used to initiate job parameters for Spring Batch jobs.

    The problem is in how I want to use it. When the value expression of property value element returns this wrapper class, but the type of the property to which it is applied is different, I want Spring to automatically extract the value from the wrapper and try to use that instead. Most of the times this will succeed because the wrapper wraps a value of exactly the type to be set.

    I've read about Spring type conversion implemented in this release. It seems to be based on property editor support of Java beans. Property editors apply to a property of a particular type, while in my case I need a converter which applies to a value of a particular type.

    Is there any way this can be implemented in a transparent way?

    Thanks.

    Denis

  • #2
    Worked-around

    Worked around the problem using AspectJ: installed the aspect like the one below and added the logic which extracts a wrapped value if the supplied value is different from the expected property type. Because of the "around" advice, the advice code can change the value if needed.

    Code:
    import java.beans.PropertyDescriptor;
    import org.springframework.core.MethodParameter;
    
    public aspect TypeConverterIntercept {
    	pointcut converterProtected() : execution(protected Object org.springframework.beans.TypeConverterDelegate.convertIfNecessary(..));
    	pointcut convertIfNecessaryArgs(String propertyName, Object oldValue, Object newValue, Class requiredType, 
    			PropertyDescriptor descriptor, MethodParameter methodParam):
    		converterProtected() && args(propertyName, oldValue, newValue, requiredType, descriptor, methodParam);
    	
    	Object around(String propertyName, Object oldValue, Object newValue, Class requiredType, 
    			PropertyDescriptor descriptor, MethodParameter methodParam) :
    		convertIfNecessaryArgs(propertyName, oldValue, newValue, requiredType, descriptor, methodParam)
    	{
    		
    		newValue = ConvertInterceptorRegistry.processIntercept(propertyName, oldValue, newValue, requiredType, descriptor);
    		return proceed(propertyName, oldValue, newValue, requiredType, descriptor, methodParam);
    	}
    }

    Comment

    Working...
    X