简体   繁体   English

Spring Custom Converter-转换为Bean或不转换为Bean

[英]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 . 我正在Spring中实现Custom Converter,因此我的bean可以从java.util.Date转换为java.time.LocalDateTime I have implemented Converter already (by implementing Spring Converter interface) 我已经实现了Converter(通过实现Spring Converter接口)

Here is bean definition in @Configuration class 这是@Configuration类中的bean定义

   @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 ? 我的问题是:我应该将自定义转换器作为Java Object还是Spring Bean传递给service.addConverter In general what are the guidelines (criterias) whether to bean or not to bean in such scenarios? 通常,在这种情况下是否使用bean的准则(准则)是什么?

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. 如果您打算将此作为某种依赖项注入到您的应用程序中,并且/或者打算在多个地方重用它,那么将其注册为Bean是有意义的。 If you're not , then new ing an instance up is acceptable. 如果您不是 ,则可以new一个实例。

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. 如果你不这样做,那么new起来。

In you simple case, it does not seem to be necessary to add DateToLocalDateTimeConverter as a spring bean. 在您的简单情况下,似乎没有必要将DateToLocalDateTimeConverter添加为spring bean。

Reasons to add DateToLocalDateTimeConverter as a spring bean: DateToLocalDateTimeConverter添加为spring bean的原因:

  • If it would make the implementation of conversionService() more readable (not the case in the question example) 如果这将使conversionService()的实现更具可读性(问题示例中不是这种情况)
  • You need the DateToLocalDateTimeConverter in other beans 您需要其他bean中的DateToLocalDateTimeConverter
  • The implementation of DateToLocalDateTimeConverter itself would need to have Spring beans injected, ie using @Autowired DateToLocalDateTimeConverter的实现本身需要注入Spring Bean,即使用@Autowired

Making an object a Spring Bean makes sense as you want that this object may benefit from Spring features (injections, transaction, aop, etc...). 使对象成为Spring Bean很有意义,因为您希望该对象可以从Spring功能(注入,事务,aop等)中受益。

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() . 由于conversionService是将实例化一次的Spring bean单例,因此在其实例化期间创建DateToLocalDateTimeConverter的纯Java实例似乎很好: 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. 现在,如果以后要在其他Spring Bean中注入DateToLocalDateTimeConverter实例,则可以将其转换为Spring Bean。


For information Spring provides already this utility task in the Jsr310Converters class (included in the spring-data-commons dependency) : 作为参考,Spring已经在Jsr310Converters类中提供了此实用程序任务(包含在spring-data-commons依赖项中):

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. 您可以直接使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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