java 如何在 spring 3 / webflow 2 中注册自定义转换服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:05:54  来源:igfitidea点击:

how do I register a custom conversion Service in spring 3 / webflow 2?

javaspringspring-webflow

提问by nont

I've been trying to follow this exampleand using the referenceto guide me, but I'm having no luck.

我一直在尝试遵循这个例子并使用参考来指导我,但我没有运气。

I've defined a converter:

我定义了一个转换器:

import org.springframework.binding.convert.converters.StringToObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

public class StringToDateTwoWayConverter  extends StringToObject {
    private DateFormat format = null;

    public StringToDateTwoWayConverter () {
        super(StringToDateTwoWayConverter.class);
        format = new SimpleDateFormat("MM/dd/yyyy");

    }

    @Override
    protected Object toObject(String string, Class targetClass) throws Exception {
        Date date = null;
        try {
            date = format.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }

    @Override
    protected String toString(Object object) throws Exception {
        Date date = (Date) object;
        return format.format(date);
    }
}

and a conversionService:

和一个转换服务:

import org.springframework.binding.convert.service.DefaultConversionService;
import org.springframework.stereotype.Component;

@Component("conversionService")
public class ApplicationConversionService extends DefaultConversionService
{
    @Override
    protected void addDefaultConverters() {
        super.addDefaultConverters();        
        this.addConverter(new StringToDateTwoWayConverter());
        this.addConverter("shortDate", new StringToDateTwoWayConverter());

    }
}

and configured it:

并配置它:

<mvc:annotation-driven conversion-service="conversionService" />
<webflow:flow-builder-services id="flowBuilderServices" conversion-service="conversionService" .../>

(explicit instantiation shows the same error)

(显式实例化显示相同的错误)

However, upon startup, I'm greeted with this exception:

但是,在启动时,我遇到了这个例外:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.core.convert.ConversionService]: Could not convert constructor argument value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.core.convert.ConversionService]: Failed to convert value of type 'com.yadayada.converter.ApplicationConversionService' to required type 'org.springframework.core.convert.ConversionService'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.core.convert.ConversionService]: no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:687)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:195)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
    ... 60 more

I'm thoroughly puzzled why its not working. The conversion service implements ConversionService through its base class, so I don't see the problem. Any insight much appreciated!

我完全不明白为什么它不起作用。转换服务通过其基类实现了ConversionService,所以我没有看到问题。任何见解非常感谢!

In response to an answer below, I tried changing the service to implement the other Conversion service:

针对以下答案,我尝试更改服务以实现其他转换服务:

import org.springframework.stereotype.Component;
import org.springframework.core.convert.ConversionService;

import org.springframework.format.support.FormattingConversionService;

@Component ("conversionService")
public class ApplicationConversionService extends FormattingConversionService implements  org.springframework.core.convert.ConversionService
{
    public ApplicationConversionService() {
        this.addConverter(new StringToDateConverter2());

}
}

But now I fail the other way:

但现在我以另一种方式失败了:

Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.yadayada.converter.ApplicationConversionService] to required type [org.springframework.binding.convert.ConversionService] for property 'conversionService': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:291)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:461)
    ... 86 more

回答by axtavt

Spring MVC and Spring Webflow uses different hierarchies of type converters. So, <mvc:annotation-driven ...>requires org.springframework.core.convert.ConversionService, but <webflow:flow-builder-services ...>requires org.springframework.binding.convert.ConversionService.

Spring MVC 和 Spring Webflow 使用不同层次结构的类型转换器。所以, <mvc:annotation-driven ...>需要org.springframework.core.convert.ConversionService,但<webflow:flow-builder-services ...>需要org.springframework.binding.convert.ConversionService

回答by Pawe? Walaszek

In above code you should write super(Date.class)instead super(StringToDateTwoWayConverter.class).

在上面的代码中,您应该super(Date.class)改为编写super(StringToDateTwoWayConverter.class).