简体   繁体   中英

Spring is autowiring custom bean into its own class

I have a bean:

@Bean
public ObjectMapper jsonMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    return objectMapper;
}

The problem is that while it's being injected into my classes, at the same time it's injected into one of Spring classes since autowiring by type is performed. I don't want WRAP_ROOT_VALUE to be set in the bean that Spring class is using. Is there any options to keep Spring from autowiring it into its classes besides not using beans in this case at all?

Use the name attribute in your @Bean annotation. Then it'll only be injected into classes that make reference to this name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM