简体   繁体   English

Spring将自定义bean自动装配到自己的类中

[英]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. 问题在于,在将其注入到我的类中的同时,由于执行了按类型的自动装配,因此同时将其注入了Spring类之一。 I don't want WRAP_ROOT_VALUE to be set in the bean that Spring class is using. 我不希望在Spring类正在使用的bean中设置WRAP_ROOT_VALUE。 Is there any options to keep Spring from autowiring it into its classes besides not using beans in this case at all? 除了在这种情况下根本不使用bean之外,是否还有其他选择可以防止Spring自动将其装配到其类中?

Use the name attribute in your @Bean annotation. @Bean批注中使用name属性。 Then it'll only be injected into classes that make reference to this name. 然后,它将仅注入引用该名称的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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