The global ConversionService bean only auto create in a spring boot web application, but I also want using this bean in a none web application. I tried to manually create this bean (hope to with the same funtion as the auto created one) as the following:
@Bean
public ConversionService conversionService() {
// The auto created one's class is WebConversionService, which is a subclass of DefaultFormattingConversionService.
var conversionService = new DefaultFormattingConversionService();
return conversionService;
}
But it will cause the startup failure with exception "Failed to convert from type [java.lang.String] to type [@org.springframework.beans.factory.annotation.Value ai.basic.basicai.client.enums.RunningMode] for value [cloud-global]". RunningMode is a custom enum type, and there is a property to read from properties file through @Value annotation.
@Value("${runningMode}")
protected RunningMode runningMode;
I also tried the following way:
@Bean
public ConversionService conversionService() {
var conversionService = new DefaultFormattingConversionService();
ApplicationConversionService.configure(conversionService);
return conversionService;
}
But it not auto register my custom converters with error "No converter found capable of converting from type [ai.basic.basicai.user.entity.UserBO] to type [ai.basic.basicai.client.dto.user.UserDTO]", which can be detected by the auto created ConversionService bean.
@Mapper(config = MapStructConfig.class)
public interface UserMapper extends Converter<UserBO, UserDTO> {
UserDTO convert(UserBO userBO);
@InheritInverseConfiguration
@DelegatingConverter
UserBO invertConvert(UserDTO userDTO);
}
Env
Spring Boot: 3.1 Java: 17
Comment From: wilkinsona
If you're defining your own conversion service and you want custom converters to be added to it, you'll have to do it yourself. It sounds like you may want to use ApplicationConversionService.addBeans(FormatterRegistry, ListableBeanFactory) to help with that.
If you have any further questions, please follow up on Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.
Comment From: jaggerwang
I already tried seach on Google、Stackoverflow、Spring official docs,even asked Chat-GPT!, but still cannot get the right answer.
Comment From: wilkinsona
Please try ApplicationConversionService.addBeans as I suggested above. If that does not help, or it leads to a further question, please post a new question on Stack Overflow that includes a minimal, reproducible example.