美文网首页
spring之如何去加载第三方的Bean

spring之如何去加载第三方的Bean

作者: onnoA | 来源:发表于2020-09-17 20:12 被阅读0次

在平時的工作中,我们经常会碰到一个这样的问题,如果想要被Spring容器管理的Bean的路径不在Spring Boot 的包扫描路径下,怎么办呢?即如何去加载第三方的Bean?

image image.png

由上图可知,启动类并不能扫描到TestComponent 主键(因为启动类默认扫描的是启动类的包,以及改包下的所有包的注解)

有两种解决方案:

一、spring.factories 就显得很有必要了。

resources目录下新建META-INF包,包下新建spring.factories文件

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.onnoa.shop.common.component.TestComponent

二、使用@Import(TestComponent.class)注解

三、使用@ImportAutoConfiguration({TestComponent.class})注解

启动结果,说明该主键已经被spring扫描到了。


image

相关文章

网友评论

      本文标题:spring之如何去加载第三方的Bean

      本文链接:https://www.haomeiwen.com/subject/sksryktx.html