Ich möchte einen Spring Converter programmgesteuert in einem Spring Boot-Projekt registrieren. In früheren Frühlingsprojekten habe ich es in XML so gemacht ...
<!-- Custom converters to allow automatic binding from Http requests parameters to objects -->
<!-- All converters are annotated w/@Component -->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<ref bean="stringToAssessmentConverter" />
</list>
</property>
</bean>
Ich versuche herauszufinden, wie SpringBootServletInitializer von Spring Boot funktioniert
Update: Ich habe ein wenig Fortschritte gemacht, indem ich den StringToAssessmentConverter als Argument an getConversionService
übergeben habe. Jetzt erhalte ich einen "No default constructor found"
-Fehler für die StringToAssessmentConverter-Klasse. Ich bin nicht sicher, warum Spring den Konstruktor von @Autowired nicht sieht.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
...
@Bean(name="conversionService")
public ConversionServiceFactoryBean getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
Set<Converter> converters = new HashSet<>();
converters.add(stringToAssessmentConverter);
bean.setConverters(converters);
return bean;
}
}
Hier ist der Code für den Konverter ...
@Component
public class StringToAssessmentConverter implements Converter<String, Assessment> {
private AssessmentService assessmentService;
@Autowired
public StringToAssessmentConverter(AssessmentService assessmentService) {
this.assessmentService = assessmentService;
}
public Assessment convert(String source) {
Long id = Long.valueOf(source);
try {
return assessmentService.find(id);
} catch (SecurityException ex) {
return null;
}
}
}
Voller Fehler
Failed to execute goal org.springframework.boot:spring-boot-maven-
plugin:1.3.2.RELEASE:run (default-cli) on project yrdstick: An exception
occurred while running. null: InvocationTargetException: Error creating
bean with name
'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPo
stProcessor': Invocation of init method failed; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'conversionService' defined in
me.jpolete.yrdstick.Application: Unsatisfied dependency expressed through
constructor argument with index 0 of type
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: : Error
creating bean with name 'stringToAssessmentConverter' defined in file
[/yrdstick/target/classes/me/jpolete/yrdstick/websupport
/StringToAssessmentConverter.class]: Instantiation of bean failed; nested
exception is org.springframework.beans.BeanInstantiationException: Failed
to instantiate
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default
constructor found; nested exception is Java.lang.NoSuchMethodException:
me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>();
nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'stringToAssessmentConverter' defined in file [/yrdstick
/dev/yrdstick/target/classes/me/jpolete/yrdstick/websupport
/StringToAssessmentConverter.class]: Instantiation of bean failed; nested
exception is org.springframework.beans.BeanInstantiationException: Failed
to instantiate
[me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default
constructor found; nested exception is Java.lang.NoSuchMethodException:
me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>()
Die Antwort ist, Sie müssen lediglich Ihren Konverter als @Component
anotieren:
Dies ist mein Konvertierungsbeispiel
import org.springframework.core.convert.converter.Converter;
@Component
public class DateUtilToDateSQLConverter implements Converter<Java.util.Date, Date> {
@Override
public Date convert(Java.util.Date source) {
return new Date(source.getTime());
}
}
Wenn Spring dann konvertieren muss, wird der Konverter aufgerufen.
Meine Spring Boot Version: 1.4.1
Hier ist meine Lösung:
Eine TypeConverter-Anmerkung:
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface TypeConverter {
}
Ein Konverter-Registrar:
@Configuration
public class ConverterConfiguration {
@Autowired(required = false)
@TypeConverter
private Set<Converter<?, ?>> autoRegisteredConverters;
@Autowired(required = false)
@TypeConverter
private Set<ConverterFactory<?, ?>> autoRegisteredConverterFactories;
@Autowired
private ConverterRegistry converterRegistry;
@PostConstruct
public void conversionService() {
if (autoRegisteredConverters != null) {
for (Converter<?, ?> converter : autoRegisteredConverters) {
converterRegistry.addConverter(converter);
}
}
if (autoRegisteredConverterFactories != null) {
for (ConverterFactory<?, ?> converterFactory : autoRegisteredConverterFactories) {
converterRegistry.addConverterFactory(converterFactory);
}
}
}
}
Und dann kommentieren Sie Ihre Konverter:
@SuppressWarnings("rawtypes")
@TypeConverter
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
@SuppressWarnings("unchecked")
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnum(targetType);
}
private final class StringToEnum<T extends Enum> implements Converter<String, T> {
private Class<T> enumType;
public StringToEnum(Class<T> enumType) {
this.enumType = enumType;
}
@SuppressWarnings("unchecked")
public T convert(String source) {
return (T) Enum.valueOf(this.enumType, source.trim().toUpperCase());
}
}
}
Für Spring Boot sieht es so aus:
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
// do not replace with lambda as spring cannot determine source type <S> and target type <T>
registry.addConverter(new Converter<String, Integer>() {
@Override
public Integer convert(String text) {
if (text == null) {
return null;
}
String trimmed = StringUtils.trimWhitespace(text);
return trimmed.equals("null") ? null : Integer.valueOf(trimmed);
}
});
}
versuche dies:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Bean
public AssessmentService assessmentService(){
return new AssessmentService();
}
@Bean
public StringToAssessmentConverter stringToAssessmentConverter(){
return new StringToAssessmentConverter(assessmentService());
}
@Bean(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
Set<Converter> converters = new HashSet<Converter>();
//add the converter
converters.add(stringToAssessmentConverter());
bean.setConverters(converters);
return bean.getObject();
}
// separate these class into its own Java file if necessary
// Assesment service
class AssessmentService {}
//converter
class StringToAssessmentConverter implements Converter<String, Assessment> {
private AssessmentService assessmentService;
@Autowired
public StringToAssessmentConverter(AssessmentService assessmentService) {
this.assessmentService = assessmentService;
}
public Assessment convert(String source) {
Long id = Long.valueOf(source);
try {
return assessmentService.find(id);
} catch (SecurityException ex) {
return null;
}
}
}
}
oder wenn Ihr StringToAssessmentConverter bereits ein Spring Bean ist:
@Autowired
@Bean(name="conversionService")
public ConversionService getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
Set<Converter> converters = new HashSet<Converter>();
//add the converter
converters.add(stringToAssessmentConverter);
bean.setConverters(converters);
return bean.getObject();
}