社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
int port = 8080;
String portPrefix = "--server.port=";
for (String arg : args) {
if (arg.startsWith(portPrefix)) {
port = Integer.parseInt(arg.substring(portPrefix.length()));
}
}
SpringApplication.run(AppRegisterApplication.class, args);
try {
Runtime.getRuntime().exec("cmd /c start http://localhost:" + port);
} catch (IOException e) {
e.printStackTrace();
}
现在使用的springboot 项目大了, 每次启动都非常的慢, 所有有了以下的依赖, 加速启动:
1.虽然类路径扫描非常快,但是Spring内部存在大量的类,添加此依赖,可以通过在编译时创建候选对象的静态列表来提高大型应用程序的启动性能。
2.但是在此模式下,作为组件扫描目标的所有模块都必须使用此机制。才可以。
3.需要spring5以上才能使用,亲测有效, 依赖如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.2.0.RELEASE</version>
<optional>true</optional>
</dependency>
然后再你的启动类上加上一个注解: @Indexed ,用来表示, 你开启了索引, 会加速项目的启动速度.
原理说明:
先看官方的解释:
在项目中使用了@Indexed
之后,编译打包的时候会在项目中自动生成META-INT/spring.components
文件。
当Spring应用上下文执行ComponentScan
扫描时,META-INT/spring.components
将会被CandidateComponentsIndexLoader
读取并加载,转换为CandidateComponentsIndex
对象,这样的话@ComponentScan
不在扫描指定的package,而是读取CandidateComponentsIndex
对象,从而达到提升性能的目的.
虽然这个@Indexed
注解能提升性能,但是在使用的时候也需要注意一一下。
假设Spring应用中存在一个包含META-INT/spring.components
资源的a.jar,b.jar仅存在模式注解,那么使用@ComponentScan
扫描这两个JAR中的package时,b.jar 中的模式注解不会被识别。
请务必注意这样的问题。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!