我相信 Spring Bootstrap
- 上下文加载器监听器
- DispatcherServlet
根据指示
<context:component-scan base-package=" ..... " />
将在运行时在应用程序启动时(或每当收到指示时)执行组件扫描。
有没有一种方法可以指示编译器(可能是通过 Maven 构建插件)在构建/编译期间对带注释的 spring 组件执行一次性静态扫描,以便不执行引导组件扫描,而不会放弃使用组件注释?
作为减少启动负载和延迟的一种方式。
最佳答案
Spring 5 添加了一项新功能以提高大型应用程序的启动性能。
它会在编译时创建候选组件列表。
在此模式下,应用程序的所有模块都必须使用此机制,因为当 ApplicationContext 检测到此类索引时,它将自动使用它而不是扫描类路径。
要生成索引,我们只需要为每个模块添加以下依赖
行家:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.0.3.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
Gradle
dependencies {
compileOnly("org.springframework:spring-context-indexer:5.0.3.RELEASE")
}
此过程将生成一个 META-INF/spring.components 文件,该文件将包含在 jar 中。
关于java - Spring组件注解编译时扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25981158/