自主开发的、带有 @Component 的组件在Spring Boot 项目中不生效的解决方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自主开发的、带有 @Component 的组件在Spring Boot 项目中不生效的解决方法相关的知识,希望对你有一定的参考价值。
参考技术A(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
)
以上是关于自主开发的、带有 @Component 的组件在Spring Boot 项目中不生效的解决方法的主要内容,如果未能解决你的问题,请参考以下文章