我正在尝试启动一个已使用此注释的 springboot 应用程序。当我尝试启动应用程序时,出现以下错误:
org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException 无法推断 com.shutterfly.sbs.platform.SbsPlatformConfigurationClientConfig.getRestTemplate 的 bean 类型
代码:
@ConditionalOnMissingBean
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
原文由 Ravi Wadje 发布,翻译遵循 CC BY-SA 4.0 许可协议
@ConditionalOnMissingBean 注释仅在给定的 bean 丢失时才用于加载 bean:
只有在上下文中不存在该类型的其他 bean 时,上面的 bean 才会被 Spring 加载。另一方面,如果应用程序上下文中已经存在 SomeBean 类型的 bean,则不会创建上述 bean。
此注释派上用场的一些用例是:
参考: https ://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html