10

Let's say I just want a plain instance of ObjectMapper object. Is there any advantage to declare it as a bean?

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper();
}

Why not just make a new ObjectMapper by new ObjectMapper() every time we need it?

Or declare it as a static object?

private static final ObjectMapper mapper = new ObjectMapper();
wltheng
  • 750
  • 1
  • 11
  • 26
  • When you declare object using `Bean` spring takes over the responsibility of managing its lifespan & scope. – Atul Sharma May 16 '18 at 05:17
  • The `ObjectMapper` is thread safe and can be reused, depending on the object creation can take a long time and reusing an existing instance might be preferable then (when it is thread safe that is). Also you might not want to control the lifecycle of the object yourself but let Spring handle it for you. – M. Deinum May 16 '18 at 05:42

1 Answers1

12

Here is the API note about ObjectMapper

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls. If configuration of a mapper is modified after first usage, changes may or may not take effect, and configuration calls themselves may fail.

And here is guideline to improve jackson performance:

Reuse heavy-weight objects: ObjectMapper (data-binding) and JsonFactory (streaming API) To a lesser degree, you may also want to reuse ObjectReader and ObjectWriter instances -- this is just some icing on the cake, but they are fully thread-safe and reusable

So to summarize:

  • ObjectMapper is thread-safe, as long as you did not change your configuration on the fly

  • ObjectMapper initialization is a heavy operation

Therefore, declare your ObjectMapper as @Bean will:

  • Improve parsing performance (as you do not need to re-init the instance when parsing)

  • Reduce memory usage (less objects created)

  • Your ObjectMapper returned from @Bean method is fully configured. It is thread-safe. (But do obviously, do not modify the @Autowired instance XD)

  • Give common configuration for your application (like timezone, null fail-over config...)

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • 1
    In your opinion, is it better to declare it a bean, or a static object as mentioned in another question, https://stackoverflow.com/questions/3907929/should-i-declare-jacksons-objectmapper-as-a-static-field ? Assuming no configuration is required, just a plain `ObjectMapper` will do. – wltheng May 16 '18 at 06:36
  • 1
    It's OK to be declared as static. But in environment like Spring, IMHO you should declare it as a bean. Spring will manage its lifecycle for you. – Mạnh Quyết Nguyễn May 16 '18 at 06:40
  • One more thing is that if you have an ObjectMapper as a bean injected into other components, you can mock ObjectMapper during testing those components, which will allow testing components in the more isolated way. – Taras Velykyy May 16 '18 at 14:47
  • You would also have one configured object with the `static` approach because you can have chained functions or a static scope under that field, I don't really see advantages of a bean in your posting... I rather see disadvantages because you need Spring and have to use `@Autowired` all the time, which could be problematic when Spring is disabled for some tests. – Thomas Gotwig Oct 08 '21 at 09:12