在JsonDeserializer中自动装配:SpringBeanAutowiringSupport与HandlerInstantiator [英] Autowiring in JsonDeserializer: SpringBeanAutowiringSupport vs HandlerInstantiator
问题描述
我编写了一个自定义 JsonDeserializer
,其中包含一个自动装配的服务,如下所示:
I have written a custom JsonDeserializer
which contains an autowired service, as follows:
public class PersonDeserializer extends JsonDeserializer<Person> {
@Autowired
PersonService personService;
@Override
public Person deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
// deserialization occurs here which makes use of personService
return person;
}
}
当我第一次使用这个解串器时,我得到了作为personService的NPE没有自动装配。从查看其他SO答案(特别是这一个)看来,有两种方法可以使自动装配工作。
When I first made use of this deserializer I was getting NPEs as personService was not being autowired. From looking at other SO answers (in particular, this one) it appears there is two ways of getting the autowiring to work.
选项1是在自定义反序列化器的构造函数中使用 SpringBeanAutowiringSupport
:
Option 1 is to use SpringBeanAutowiringSupport
within the constructor of the custom deserializer:
public PersonDeserializer() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
选项2是使用 HandlerInstantiator
并使用我的 ObjectMapper
bean注册:
Option 2 is to use a HandlerInstantiator
and register it with my ObjectMapper
bean:
@Component
public class SpringBeanHandlerInstantiator extends HandlerInstantiator {
@Autowired
private ApplicationContext applicationContext;
@Override
public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<? extends JsonDeserializer<?>> deserClass) {
try {
return (JsonDeserializer<?>) applicationContext.getBean(deserClass);
} catch (Exception e) {
// Return null and let the default behavior happen
return null;
}
}
}
@Configuration
public class JacksonConfiguration {
@Autowired
SpringBeanHandlerInstantiator springBeanHandlerInstantiator;
@Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
jackson2ObjectMapperFactoryBean.afterPropertiesSet();
ObjectMapper objectMapper = jackson2ObjectMapperFactoryBean.getObject();
// add the custom handler instantiator
objectMapper.setHandlerInstantiator(springBeanHandlerInstantiator);
return objectMapper;
}
}
我尝试了两种选择,但它们同样有效。显然,选项1更容易,因为它只有三行代码,但我的问题是:与 HandlerInstantiator <相比,使用
SpringBeanAutowiringSupport
是否有任何缺点? / code>方法?我的应用程序将每分钟反序列化数百个对象,如果这有任何不同。
I have tried both options and they work equally well. Clearly option 1 is much easier as it's only three lines of code, but my question is: are there any disadvantages to using SpringBeanAutowiringSupport
compared to the HandlerInstantiator
approach? My application is going to be deserializing hundreds of objects per minute, if that makes any difference.
任何建议/反馈都表示赞赏。
Any advice/feedback is appreciated.
推荐答案
添加到Amir Jamak的答案中,您不必像Spring那样创建自定义HandlerInstantiator,它就是SpringHandlerInstantiator。
Adding to the Amir Jamak's answer, you don't have to create custom HandlerInstantiator as Spring has it already which is SpringHandlerInstantiator.
你需要做的是在Spring配置中将它连接到Jackson2ObjectMapperBuilder。
What you need to do is to hook it up to Jackson2ObjectMapperBuilder in Spring configuration.
@Bean
public HandlerInstantiator handlerInstantiator(ApplicationContext applicationContext) {
return new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory());
}
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder(HandlerInstantiator handlerInstantiator) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.handlerInstantiator(handlerInstantiator);
return builder;
}
这篇关于在JsonDeserializer中自动装配:SpringBeanAutowiringSupport与HandlerInstantiator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!