8

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

PS。现在我通过ConversionServiceFactoryBean在配置文件中定义/注入它们来使用我的转换器

4

3 回答 3

19

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

但是 spring 3 还有其他几个选项:

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

  2. 否则,您可以实现自己的org.springframework.core.convert.converter.GenericConverter,这使得使用反射创建 TwoWayConverter 实现变得容易。

    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();
        }
    }
    
于 2012-11-12T14:15:26.910 回答
4

Spring 为此目的提供了这样一个接口:TwoWayConverter。请参阅以下内容: http ://static.springsource.org/spring-webflow/docs/2.0.x/javadoc-api/org/springframework/binding/convert/converters/TwoWayConverter.html

于 2012-06-13T05:33:09.060 回答
1

您可以使用Spring Formatter将 T 类型的对象格式化为 String ,反之亦然。

package org.springframework.format;

public interface Formatter<T> extends Printer<T>, Parser<T> {
}

使用此接口,您可以实现与 Barry Pitman 所说的相同,但代码更少,如果您想格式化为字符串,这是 Spring 文档的首选方式,反之亦然。所以 Barry 的 UserIdConverter 类看起来像这样:

public class UserIdConverter implements Formatter<User> {

    private final UserDao userDao;

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

    @Override
    public User parse(String userId, Locale locale) {
        return userDao.load(userId);
    }

    @Override
    public String print(User target, Locale locale) {
        return target.getUserId();
    }
}

要注册此格式化程序,您应该将其包含在您的 XML 配置中:

...
<mvc:annotation-driven conversion-service="conversionService"/>

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
    <property name="formatters">
        <set>
            <bean class="com.x.UserIdConverter"/>
        </set>
    </property>
</bean>
...

!请注意,此类只能用于从某种类型 T 格式化为 String ,反之亦然。例如,您不能从类型 T 格式化为其他类型 T1。如果你有这种情况,你应该使用 Spring GenericConverter 并使用 Barry Pitman 的答案:

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();
    }
}

并添加到您的 XML 配置中:

...
<mvc:annotation-driven conversion-service="conversionService"/>

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
    <property name="converters">
        <set>
            <bean class="com.x.y.UserIdConverter"/>
        </set>
    </property>
</bean>
...
于 2016-02-24T12:01:53.000 回答