项目引用了业务日志库,使用注解 @EnableLogRecord 来自动配置库相关的 Bean ,其中有个获取当前操作人的 Bean 有 @ConditionalOnMissingBean 注解,当自定义该 Bean 的时候,以下第一种情况符合预期,第二种会启动失败,想请教下各位其中的原因及解决办法
库源码:com.mzt.logapi.starter.configuration.LogRecordProxyAutoConfiguration
@Bean
@ConditionalOnMissingBean(IOperatorGetService.class)
@Role(BeanDefinition.ROLE_APPLICATION)
public IOperatorGetService operatorGetService() {
return new DefaultOperatorGetServiceImpl();
}
一. 在 spring boot 启动类添加 @EnableLogRecord ,@ConditionalOnMissingBean 有效
@EnableLogRecord(tenant = "temp")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
二. 在配置类添加 @EnableLogRecord ,@ConditionalOnMissingBean 无效,启动报错
@EnableLogRecord(tenant = "temp")
@Configuration(proxyBeanMethods = false)
public class LogRecordConfiguration {
public LogRecordConfiguration() {
}
/**
* 自定义获取操作人
*/
@Bean
public IOperatorGetService operatorGetService() {
return () -> {
Operator operator = new Operator();
operator.setOperatorId("test");
return operator;
};
}
}
repo: https://github.com/TheLastSunset/spring-boot-ConditionalOnMissingBean-issues.git
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.