Spring bean overriding does not throw exception when default settings is not allowing
Solution 1:
Because of the @ConditionalOnMissingBean
in the default ObjectMapper
bean configuration provided by spring-boot , it will only define and configure a default ObjectMapper
bean if you do not define one by yourself.
Now as you define an ObjectMapper
, it causes spring-boot stop defining the default one. So in the end you just have one ObjectMapper
bean and hence BeanDefinitionOverrideException
will not happen.
If you create another @Configuration
and define one more ObjectMapper
with the same name , you should produce BeanDefinitionOverrideException
:
@Configuration
@Import(BeanConfig2.class)
public class BeanConfig {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
@Configuration
public class BeanConfig2 {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}