【发布时间】:2017-09-19 08:18:08
【问题描述】:
我想创建自己的jacksonObjectMapper bean,如下:
@SpringBootApplication
@AutoConfigureBefore(JacksonAutoConfiguration.class) //even this does not help
public class MyConfig extends SpringBootServletInitializer {
@Bean
@Primary
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(true).build(); //much more configurations
}
}
问题:从未创建 bean,而是执行默认的 JacksonAutoConfiguration:
package org.springframework.boot.autoconfigure.jackson;
@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class JacksonAutoConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build(); //this is always executed!
}
}
所以在评估JacksonAutoConfiguration 时,不知何故ObjectMapper bean 还不存在。但为什么呢?
通过断点调试,我还可以看到我的 bean 从不被调用!
但我注意到:即使我有@AutoConfigureBefore,jackson 自动配置仍然在MyConfig 中的任何 bean 之前运行。奇怪吗?
【问题讨论】:
-
只是澄清一下:你在那里放置了调试器断点,它永远不会被调用,对吧?
-
没错,我的
ObjectMapperbean 完全被忽略了(同一个MyConfig类中的所有其他bean 并非如此。 -
您能否为您的问题提供更多背景信息?从您所展示的内容来看,尚不清楚应该使用
MyConfig。您是否在 IDE 中将应用程序作为 jar、war 或其他方式运行?如果它有一个,它的主要方法是什么样的? minimal, complete, verifiable, example 会让事情变得更清晰。
标签: java spring spring-boot jackson