0
点赞
收藏
分享

微信扫一扫

自主开发的、带有 @Component 的组件在Spring Boot 项目中不生效的解决方法

橙子好吃吗 2021-09-25 阅读 37

(1)问题描述

开发了一套组件,以 jar 形式发布。然后在 Spring Boot 项目中使用 pom引用后,无法调用到在组件中用 @Component 注释的类。

(2)原因分析

默认情况下,@ComponentScan注解会扫描当前包及其所有子包中的组件。而 @SpringBootApplication 注解包含了@ComponentScan,所以 Spring Boot 框架会自动扫描 Spring Boot启动类当前包及其所有子包中的组件类。而我们的组件因为不在自动扫描范围内,所以无效。

(3)问题解决

在 Spring Boot启动类中使用 @Import 注解,导入我们希望 Spring Boot 扫描的类。如果有多个类以逗号分隔。形如:
@Import(value = { ds.interceptor.SwitchDataSourceInterceptor.class,
ds.config.DatasourceMapperConfig.class,
ds.service.RSAServiceImpl.class
})

举报

相关推荐

0 条评论