xml – 在Spring 3中注册带注释的转换器和converterFactories
首先……我在
Spring中相对较新,我使用spring 3.x而且我不喜欢Spring的XML配置文件…我不希望我做的每一次重构,都要运行XML文件进行更新……
我试图以一种方式配置spring,对于任何请求,如果我在我的hadlers中有一些@RequestParam / @RequestBody / @ PathVariable等类型而不是String类型,spring会正确地将值转换为该类型或者将null放入handler’s args(我从不在处理程序参数中使用原始类型).到现在为止还挺好 … 到目前为止,我已经注册了所有的converter / converterFactory类,如下所示: <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <!-- converters is a set of both converters and converterfactories --> <bean class="controller.converters.enumConverter" /> <bean class="controller.converters.integerConverter" /> <bean class="controller.converters.objects.FooConverter" /> ... </list> </property> </bean> 有没有办法注册带注释的转换器? 关于spring XML的任何东西(或者只是基本的东西)都可以仅使用注释完成,并且一劳永逸地摆脱XML配置吗? … 如何?
Spring没有对转换器的注释支持,但您可以构建自己的.
您只需要一个自定义限定符注释(让我们称之为@AutoRegistered)和某种类型的Converter / Formatter Registrar(实现 然后你需要用这个注释(以及其他一些注释来注释你的对流器) @AutoRegistered注释: @Target({ ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface AutoRegistered {} 报名服务: import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistrar; import org.springframework.format.FormatterRegistry; public class AutoregisterFormatterRegistrar implements FormatterRegistrar { /** * All {@link Converter} Beans with {@link AutoRegistered} annotation. * If spring does not find any matching bean,then the List is {@code null}!. */ @Autowired(required = false) @AutoRegistered private List<Converter<?,?>> autoRegisteredConverters; @Override public void registerFormatters(final FormatterRegistry registry) { if (this.autoRegisteredConverters != null) { for (Converter<?,?> converter : this.autoRegisteredConverters) { registry.addConverter(converter); } } } } 注册商的XML配置: <bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatterRegistrars"> <set> <bean class="AutoregisterFormatterRegistrar" autowire="byType" /> </set> </property> </bean> BTW为您的枚举转换器,您不需要ConversionFactory – 一个简单的转换器就足够了: @AutoRegistered @Component public class EnumConverter implements Converter<Enum<?>,String> { /** Use the same immutable value instead of creating an new array every time. */ private static final Object[] NO_PARAM = new Object[0]; /** The prefix of all message codes. */ private static final String PREFIX = "label_"; /** The separator in the message code,between different packages as well as between package can class. */ private static final String PACKAGE_SEPARATOR = "_"; /** The separator in the message code,between the class name and the enum case name. */ private static final String ENUM_CASE_SEPARATOR = "_"; /** The message source. */ private MessageSource messageSource; @Autowired public EnumConverter(final MessageSource messageSource) { if (messageSource == null) { throw new RuntimeException("messageSource must not be null"); } this.messageSource = messageSource; } @Override public String convert(final Enum<?> source) { if (source != null) { String enumValueName = source.name(); String code = PREFIX + source.getClass().getName().toLowerCase(). replace(".",PACKAGE_SEPARATOR) + ENUM_CASE_SEPARATOR + enumValueName.toLowerCase(); String message = messageSource.getMessage(code,NO_PARAM,enumValueName,LocaleContextHolder.getLocale()); return message; } else { return ""; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |