服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 优化技巧:如何加快Spring项目启动速度

优化技巧:如何加快Spring项目启动速度

2023-12-06 14:06Spring全家桶实战案例源码 Java教程

在大型Spring项目中,由于Bean数量众多,导致启动速度变慢。使用spring-context-indexer可以优化启动速度,提高开发效率、减少资源占用和减少故障、错误率。spring-context-indexer是一个工具,它可以在编译时为类路径下的组件创建索引,

环境:Spring5.3.23

1. 介绍

在大型的Spring项目中,由于有成百上千的Bean需要通过扫描注册到Spring容器中,这会导致启动速度变慢。为了解决这个问题,我们可以使用spring-context-indexer来优化启动速度。

spring-context-indexer是一个工具,它可以在编译时为类路径下的组件创建索引,这样在启动时就可以通过索引快速地加载和初始化组件。使用spring-context-indexer可以大大提升Spring应用程序的启动速度,从而使得开发人员可以更快地开发和测试应用程序,提高开发效率。

在大型项目中,由于Bean数量众多,Spring应用程序的启动时间可能会变得非常长。通过使用spring-context-indexer,我们可以减少启动时间,从而减少对系统资源的占用,使得更多的资源可以被用来处理其他任务。此外,快速启动应用程序还可以减少因为程序长时间未响应而导致的故障和错误率。

2. 配置使用

引入依赖包


  org.springframework
  spring-context-indexer
  5.3.23
  true

如果使用的是gradle

# gradle 4.5以下版本包括4.5
dependencies {
  compileOnly "org.springframework:spring-context-indexer:5.3.23"
}
# gradle 4.6以上版本
dependencies {
  annotationProcessor "org.springframework:spring-context-indexer:5.3.23"
}

准备Bean对象

@Component
public class Person {
}
@Component
public class Student {
}
@Component
public class User {
}

测试上面的的类都能被容器扫描到

try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.pack.context_indexed")) {
    for (String name : context.getBeanDefinitionNames()) {
      System.out.println(name) ;
    }
  }
}

控制台输出

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalPersistenceAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
person
student
user

所有的bean都能被容器扫描到

手动创建META-INF/spring.components 文件

内容如下

com.pack.context_indexed.Person=org.springframework.stereotype.Component

格式:完整的包名=完整注解名

有了上面的索引文件后,再次运行上面的测试文件

# ...
person

自定义的bean就只剩下person了,这就是因为在上面的索引文件中只定义了    person的原因,这样就不会在扫描你当前包下的所有class文件了,只会读取索引文件中的内容。

此时如果你访问不在此列表中的类,程序将报错,找不到对应的Bean对象。

自定义注解支持

我们可以在索引文件中使用自己定义的注解,示例如下

// 自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface PackComponent {
}
// 修改User类注解
@PackComponent
public class User {
}

在索引文件中添加配置

com.pack.context_indexed.Person=org.springframework.stereotype.Component
com.pack.context_indexed.User=com.pack.context_indexed.PackComponent

控制台输出

# ...
person
user

以上都是通过手动创建的方式,在实际大型项目中如果你手动创建维护索引文件那还不如不使用索引,并且还及其容易出现错误。我们可以借助IDE工具配置注解处理器来帮我们自动的完成索引文件的创建。

这里以Eclipse为例来配置

首先,将spring-context-indexer添加eclipse注解处理中

优化技巧:如何加快Spring项目启动速度图片

通过上面的1,2,3步后,索引文件将会被自动的生成。

自动生成的spring.components文件,默认将在target\classes\META-INF目录下,部分内容:

com.pack.context_indexed.Persnotallow=org.springframework.stereotype.Component
com.pack.context_indexed.Student=org.springframework.stereotype.Component
com.pack.context_indexed.User=org.springframework.stereotype.Component

关闭索引功能

我们可以通过设置JVM参数进行关闭索引功能,在启动程序添加如下参数即可关闭

-Dspring.index.ignore=true

在大型Spring项目中,由于Bean数量众多,导致启动速度变慢。使用spring-context-indexer可以优化启动速度,提高开发效率、减少资源占用和减少故障、错误率。spring-context-indexer是一个工具,它可以在编译时为类路径下的组件创建索引,这样在启动时就可以通过索引快速地加载和初始化组件。

原文地址:https://mp.weixin.qq.com/s/ef323o9l92BvbtPtwGTCFA

延伸 · 阅读

精彩推荐