Spring上下文索引器不适用于依赖罐

Mah*_*uva 5 java spring spring-boot

我有一些库jar lib.jar(使用spring boot创建,但打包为没有spring boot插件的普通jar),它由spring boot组成,包含spring.components由生成的文件spring-context-indexer

现在,我在我的应用程序中使用了这个jar,它也有spring-context-indexer一个自己的spring.components文件,并使用了lib.jar中定义的一些bean 。

当我开始我的应用程序,春季应登记中定义的所有豆类spring.componentslib.jarspring.components应用。但是spring没有注册lib.jar的任何bean 。

我尝试使用的basePackages属性,@SpringBootApplication但没有结果。

我甚至复制的所有条目spring.componentslib.jarspring.components我的应用程序,但没有结果。

谁能帮帮我吗?

Rom*_*sov 0

长话短说

如果您正在使用 Spring Data,@SpringBootApplication.scanBasePackages这还不够,您还需要@EnableJdbcRepositories(或*Jpa*或其他)。

package application;


// without this annotation all Repository classes
// from library will be missing
@EnableJdbcRepositories({ 
        "application",
        "library"
})
@SpringBootApplication(
        scanBasePackages = {
                "application",
                "library"
        }
)
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

更多信息

好吧,也许我有点晚了,但我决定稍微调查一下这个案子。这是我截至 2022 年 2 月 2 日的发现:

  1. 所有META-INF/spring.components文件都加载到CandidateComponentsIndexLoader.doLoadIndex. 您可以使用 debug 来检查它是否看到 lib 中的文件

  2. CandidateComponentsIndexLoader然后创建CandidateComponentsIndex,然后将其存储在组件扫描仪中,对我来说它是AnnotationConfigServletWebServerApplicationContext.scanner.componentsIndex

  3. ClassPathScanningCandidateComponentProvider findCandidateComponents然后调用in ,如果componentsIndex不为 null,则仅通过提供的索引从该索引获取组件basePackage

这就是为什么缺失basePackage至关重要。

我还没有深入研究 Spring Data 算法,但就我而言,在我添加 with 包之前,Spring 还没有生成库存储库@EnableJdbcRepositories

PS 所有链接均代表 5.3.15 标签、latest atm 处的文件。