gpt4 book ai didi

grails - Spring 转换器内的Grails自动接线

转载 作者:行者123 更新时间:2023-12-02 15:28:13 26 4
gpt4 key购买 nike

我有这样的东西...

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,以便仍然可以 Autowiring

最佳答案

查看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。

关于grails - Spring 转换器内的Grails自动接线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24417095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com