Spring對轉換器沒有註釋支持,但可以自己構建。
所有你需要的是一個自定義的限定註釋(可以稱之爲@AutoRegistered
)和某種轉換器/格式化註冊處處長(實現FormatterRegistrar
)的,其登記所有的Spring Bean與此@AutoRegistered
註釋(和一些XML註冊該註冊服務) 。
然後你需要使用這個註解(以及其他一些註釋使它成爲spring bean的 )來註釋你的conveter,這就是全部。
@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 "";
}
}
}
FormatterRegistrar?我想你的意思是FormatterRegistry ... Is List> autoRegisteredConverters自動填充? ps。 ConverterFactory的枚舉解決方案更加緊湊...檢查這[實施](http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#core-convert -ConverterFactory-SPI) –
ApollonDigital
它是FormatterRegistrar(Spring 3.1特性)http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/format/FormatterRegistrar.html – Ralph