弹簧双向转换器 [英] Two-way converter in spring

查看:163
本文介绍了弹簧双向转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3有一个很好的功能,如类型转换。它提供了一个转换器SPI(转换器< S,T> ),用于实现差分转换逻辑。
Converter类型的子类允许定义单向转换(仅从S到T),所以如果我想要从T到SI执行转换需要定义另一个实现<$ c $的转换器类c>转换器< T,S> 。如果我有许多可以转换的类,我需要定义许多转换器。
是否有可能在一个转换器中定义双向转换逻辑(从S到T和从T到S)?以及它将如何使用?

Spring 3 has such a nice feature as type conversion. It provides a converter SPI(Converter<S, T>) to be used to implement differenet conversion logic. The subclass of Converter type allow to define one-way conversion(only from S to T), so if I want a conversion also to be performed from T to S I need to define another converter class that implement Converter<T, S>. If I have many classes which are subject to conversion, i need to define many converters. Is there any posibility to define two-way conversion logic(from S to T and from T to S) in one converter? and how it will be used?

PS。现在我通过 ConversionServiceFactoryBean 在配置文件中定义/注入它们

PS. now I'm using my converters via ConversionServiceFactoryBean defining/injecting them in configuration file

推荐答案

你是对的,如果你想直接使用 org.springframework.core.convert.converter.Converter 接口,你需要实现两个转换器,每个方向一个。

You are correct, if you want to use the org.springframework.core.convert.converter.Converter interface directly, you'll need to implement two converters, one for each direction.

但是春天3有几个其他选择:

But spring 3 has a couple of other options:


  1. 如果您的转换不是对象到对象,而是对象到字符串(和返回),那么您可以实现 org.springframework.format.Formatter 相反。 Formatters注册为GenericConverters(参见 http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch05s07.html#converter-upgrade-to-spring-3

  1. If your conversion is not object-to-object but rather object-to-string (and back), then you can implement a org.springframework.format.Formatter instead. Formatters get registered as GenericConverters (see http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch05s07.html#converter-upgrade-to-spring-3)

否则你可以实现自己的 org.springframework.core.convert.converter.GenericConverter ,这使得它成为可能使用反射很容易创建TwoWayConverter实现。

Otherwise you could implement your own org.springframework.core.convert.converter.GenericConverter, which makes it easy to create TwoWayConverter implementations using reflection.

public abstract class AbstractTwoWayConverter<S, T> implements GenericConverter {

    private Class<S> classOfS;
    private Class<T> classOfT;

    protected AbstractTwoWayConverter() {
        Type typeA = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        Type typeB = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        this.classOfS = (Class) typeA;
        this.classOfT = (Class) typeB;
    }

    public Set<ConvertiblePair> getConvertibleTypes() {
        Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>();
        convertiblePairs.add(new ConvertiblePair(classOfS, classOfT));
        convertiblePairs.add(new ConvertiblePair(classOfT, classOfS));
        return convertiblePairs;
    }

    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (classOfS.equals(sourceType.getType())) {
            return this.convert((S) source);
        } else {
            return this.convertBack((T) source);
        }
    }

    protected abstract T convert(S source);

    protected abstract S convertBack(T target);

}

/** 
 * converter to convert between a userId and user.
 * this class can be registered like so: 
 * conversionService.addConverter(new UserIdConverter (userDao));
 */ 
public class UserIdConverter extends AbstractTwoWayConverter<String, User> {

    private final UserDao userDao;

    @Autowired
    public UserIdConverter(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    protected User convert(String userId) {
        return userDao.load(userId);
    }

    @Override
    protected String convertBack(User target) {
        return target.getUserId();
    }
}


这篇关于弹簧双向转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆