简体   繁体   中英

Spring Custom Converter - To Bean or Not to Bean

I am implementing Custom Converter in Spring so my beans can convert from java.util.Date to java.time.LocalDateTime . I have implemented Converter already (by implementing Spring Converter interface)

Here is bean definition in @Configuration class

   @Bean
   ConversionService conversionService(){
      DefaultConversionService service = new DefaultConversionService();
      service.addConverter(new DateToLocalDateTimeConverter());
      return service;
    }

My question is : shall I pass my custom converter as Java Object or Spring Bean to service.addConverter ? In general what are the guidelines (criterias) whether to bean or not to bean in such scenarios?

If you intend to inject this as a dependency of some kind into your application, and/or you intend to reuse it in multiple places, then it makes sense to register it as a bean. If you're not , then new ing an instance up is acceptable.

Dependency injection and inversion of control are just that - how you inject dependencies into your app, and an acknowledgment that you no longer control how that's instantiated. Should you desire either of these, beans are suitable; if you don't, then new it up.

In you simple case, it does not seem to be necessary to add DateToLocalDateTimeConverter as a spring bean.

Reasons to add DateToLocalDateTimeConverter as a spring bean:

  • If it would make the implementation of conversionService() more readable (not the case in the question example)
  • You need the DateToLocalDateTimeConverter in other beans
  • The implementation of DateToLocalDateTimeConverter itself would need to have Spring beans injected, ie using @Autowired

Making an object a Spring Bean makes sense as you want that this object may benefit from Spring features (injections, transaction, aop, etc...).

In your case, it seems not required.
As conversionService is a Spring bean singleton that will be instantiated once, creating during its instantiation a plain java instance of DateToLocalDateTimeConverter seems fine : new DateToLocalDateTimeConverter() .

Now, if later you want to inject the DateToLocalDateTimeConverter instance in other Spring beans, it would make sense to transform it to a Spring Bean.


For information Spring provides already this utility task in the Jsr310Converters class (included in the spring-data-commons dependency) :

import static java.time.LocalDateTime.*;

public abstract class Jsr310Converters {
...
   public static enum DateToLocalDateTimeConverter implements Converter<Date, LocalDateTime> {

      INSTANCE;

      @Override
      public LocalDateTime convert(Date source) {
          return source == null ? null : ofInstant(source.toInstant(), ZoneId.systemDefault());
      }
  }
 ...
}

You could directly use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM