Answer the question
In order to leave comments, you need to log in
Spring Boot: how to create custom bean without affecting autoconfiguration behavior?
Question for Spring Boot experts.
I need to inject a custom ObjectMapper into my component, but at the same time I don’t want to break the default mapper created automatically in JacksonAutoConfiguration , because in all other places in the application the default version is used, supplemented with settings from application.yml / bootstrap.yml : all kinds of custom serialization/deserialization settings for dates, enums, etc. That is, I do not want to touch the default option, because it is already correctly configured to work with the standard settings options.
I create my config :
@Configuration
public class CustomMapperConfig {
@Bean(name = "myCustomMapper")
public ObjectMapper myCustomMapper() {
ObjectMapper mapper = new ObjectMapper();
//do some customizing
return mapper;
}
}
@Component
public class MyComponent {
@Autowired @Qualifier("myCustomMapper")
ObjectMapper myCustomMapper; // хочу использовать тут свой бин
@Autowired
ObjectMapper objectMapper;
// хочу тут использовать стандартный бин из автоконфига,
// но инжектится все равно моя кастомная реализация
}
@Bean
@Primary
@ConditionalOnMissingBean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
}
@Configuration
@AllArgsConstructor
public class CustomMapperConfig {
private final JacksonAutoConfiguration autoConfiguration;
@Bean(name = "myCustomMapper")
@Order(Ordered.LOWEST_PRECEDENCE)
public ObjectMapper myCustomMapper() {
//...
}
}
@Configuration
@AllArgsConstructor
public class CustomMapperConfig {
private final ObjectMapper defaultMapper;
@Bean(name = "myCustomMapper")
public ObjectMapper myCustomMapper() {
//customize
return defaultMapper;
}
}
@Configuration
public class CustomMapperConfig {
@Bean(name = "myCustomMapper")
public ObjectMapper myCustomMapper(ObjectMapper defaultMapper) {
//customize
return defaultMapper;
}
}
The dependencies of some of the beans in the application context form a cycle: customMapperConfig defined in file...
Answer the question
In order to leave comments, you need to log in
I can suggest this option:
@Configuration
public class CustomMapperConfig {
public CustomMapperConfig(ApplicationContext context) {
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
//customize
}
}
context.getAutowireCapableBeanFactory()
public CustomMapperConfig(ObjectMapper objectMapper) {
//customize
}
The other day I was also digging with a similar question.
So, autoconfiguration always works after those configs that are defined in the application.
As for the question, I would try
@Bean(name = "myCustomMapper")
@DependsOn({"objectMapper"})
public ObjectMapper myCustomMapper(@Lazy @Qualifier("objectMapper") ObjectMapper defaultMapper) {
//customize
return defaultMapper;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question