Spring转换器内的Grails autowire

时间:2014-06-25 19:36:11

标签: grails

我有这样的事情......

class DomainConverter implements GenericConverter {
    private Class<Domain> classOfDomain;
    private Class<DomainCommand> classOfDomainCommand;
    public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() {
        Set<GenericConverter.ConvertiblePair> convertiblePairs = new HashSet<GenericConverter.ConvertiblePair>();
        convertiblePairs.add(new GenericConverter.ConvertiblePair(classOfDomain, classOfDomainCommand));
        convertiblePairs.add(new GenericConverter.ConvertiblePair(classOfDomainCommand, classOfDomain));
        return convertiblePairs;
    }
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (classOfDomain.equals(sourceType.getType())) {
            return this.convert((Domain) source);
        } else {
            return this.convertBack((DomainCommand) source);
        }
    }
    protected DomainCommand convert(Domain definition){
        ...
    }
    protected Domain convertBack(DomainCommand command){
        ...
    }
}

我是否必须在课堂内连线或将其添加到resources.groovy?

如何创建DomainCommand以便我仍然拥有自动装配

1 个答案:

答案 0 :(得分:1)

查看Spring documentation,您可以看到bean声明的示例:

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean"/>

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="example.MyCustomConverter"/>
        </list>
    </property>
</bean>

因此,您需要在resources.groovy文件中声明转换器以及conversionService。