【问题标题】:Spring component annotation compile time scanSpring组件注解编译时扫描
【发布时间】:2014-09-22 18:50:36
【问题描述】:

我的信念是 Spring 引导

  • ContextLoaderListener
  • DispatcherServlet

由于指令

<context:component-scan base-package=" ..... " />

将在应用启动时(或在运行时收到指示时)执行组件扫描。

有没有办法指示编译器(也许通过一个 maven 构建插件)在构建/编译期间对带注释的 spring 组件执行一次静态扫描,以便不执行引导组件扫描,而不放弃使用组件注释?

作为一种减少启动负载和延迟的方法。

【问题讨论】:

  • 那会很酷。我没有听说过这样的插件。然后,您必须在运行时禁用您的配置。
  • 首先控制框架的反转会增加启动延迟,但仅此而已...除非您的应用程序需要很长时间才能启动,否则没有必要这样做。其次,我们放弃了 XML 配置和组件扫描,因为我们发现 Spring Java 配置更加明确......作为额外的奖励启动时间改进
  • 缺少“无关紧要”的信息:我正在努力将应用程序移至 google-app-engine。正如 gae 建议将尽可能多的活动移到编译时间。但我希望这是一个与 gae 无关的一般性问题。
  • 我也有同样的担忧。部署时间很重要:可能是部署/调试/重试所需的时间:在构建时生成 context.xml 肯定会改善工作流程时间。我认为如果创建一个过滤 context.xml 资源并将 扩展为单个 bean 声明的 maven 插件并不难。

标签: java spring spring-mvc startup


【解决方案1】:

Spring 5添加了一项新功能以提高大型应用程序的启动性能。

它在编译时创建一个候选组件列表。

在这种模式下,应用程序的所有模块都必须使用这种机制,因为当ApplicationContext检测到这样的索引时,它会自动使用它而不是扫描类路径。

要生成索引,我们只需要为每个模块添加以下依赖项

Maven:

<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 中。

参考:1.10.9. Generating an index of candidate components

【讨论】:

【解决方案2】:

Spring 5 添加了generating an index of candidate components at compile time 选项。找到索引后,仅使用索引并跳过完整的类路径扫描。

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2015-01-13
    • 1970-01-01
    • 2013-12-23
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多