如何使用带有Grails服务的Custom Spring Converter

时间:2014-06-25 20:44:22

标签: grails spring-4

我有一个GenericConverter类型的自定义Spring Converter。我尝试将它连接到这样的服务......

resources.groovy
domainConverter(DomainConverter){bean -> bean.scope = 'prototype'; bean.autowire = "byName"}

class DomainService {
    def domainConverter
    DomainCommand convertDomain(Domain bd){
       bd as DomainCommand
    }

    Domain convertCommand(DomainCommand bc){
        bc as Domain
    }
}

然而这似乎失败了......

  

无法将类“Domain”的对象“Domain(...)”强制转换为class   'DomainCommand'

有人可以帮助解决我所缺少的问题吗?

1 个答案:

答案 0 :(得分:1)

domainConverter(DomainConverter){bean -> bean.scope = 'prototype'; bean.autowire = "byName"}
domainConverterService(ConversionServiceFactoryBean){
  converters = ref("domainConverter")
}

在役......

def domainConverterService
def newCommand = domainConverterService.convert(domain, domain.class)