记录一次@ConditionalOnBean导致项目启动报错问题

1、当我再springboot 项目中通过

@ConditionalOnBean

去调用es使,发现项目报错找不到对应service的而启动失败。

报错信息

Parameter 21 of constructor in cn.ac.iscas.pdm.biz.rest.dev.service.workbench.impl.AgencyCenterServiceImpl required a bean of type 'cn.ac.iscas.pdm.biz.rest.dev.service.regulationsystem.BorrowReadRegulationsService' that could not be found.

 

2、初步才是使bean的顺序加载问题

@Slf4j
@Api(tags = "xxx")
@RequiredArgsConstructor
@RequestMapping("/xxxx")
@ConditionalOnBean({ ElasticsearchHandler.class})
public class BorrowReadRegulationsController extends BaseController {

    private final BorrowReadRegulationsService borrowReadRegulationsService;

}

3、解决方法:

统一再这个类加上注解调整springBoot加载bean的排序即可解决

@Configuration
@RestController
@Slf4j
@Api(tags = "xxx")
@RequiredArgsConstructor
@RequestMapping("/xxx")
@Configuration
@ConditionalOnBean({ ElasticsearchHandler.class})
public class BorrowReadRegulationsController extends BaseController {

    private final BorrowReadRegulationsService borrowReadRegulationsService;

    private final TableDefinitionService tableDefinitionService;
}

正常启动

请登录后发表评论

    没有回复内容