@ConditionalOnMissingBean(RulesEngine.class)
时间: 2023-07-26 10:51:59 浏览: 83
这是一个Spring Boot注解,它的作用是当Spring容器中不存在类型为RulesEngine的Bean时,才会将带有此注解的Bean注册到容器中。这个注解通常用于给默认实现提供一个备选方案。如果容器中已经存在了类型为RulesEngine的Bean,则Spring Boot不会再创建一个新的实例。
相关问题
@ConditionalOnMissingBean(ObjectMapper.class)
@ConditionalOnMissingBean(ObjectMapper.class)是一个注解,它的作用是在一个bean被注册后,如果后面注册了相同类型的bean,就不会成功。它确保了这个类型的bean只有一个实例。当出现相同的bean时,使用@ConditionalOnMissingBean注解会出现异常。 <span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [jackson 中对null的处理](https://blog.csdn.net/weixin_45289374/article/details/115121485)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [SpringBoot使用Jackson序列化反序列化配置](https://blog.csdn.net/hcksust/article/details/112427476)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
@Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } }
这是一个Java类,用于配置Jackson库的ObjectMapper对象。ObjectMapper是Jackson库中最核心的类之一,用于将Java对象转换为JSON格式的字符串或将JSON格式的字符串转换为Java对象。在这个类中,使用了@Bean注解来定义一个名为jacksonObjectMapper的Bean,并且使用了@Primary和@ConditionalOnMissingBean注解来保证这个Bean是首选的ObjectMapper,并且仅在没有其他ObjectMapper Bean存在时才创建。在这个Bean中,设置了一个NullValueSerializer,用于将Java对象中的null值序列化为一个空字符串。
阅读全文