Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of spring-context-indexer dependency in Spring

Shulou Source: shulou.com Published: 2022-06-02 16:48:28 09月28日 Update

This article is about the usefulness of spring-context-indexer dependency in Spring. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Spring spring-context-indexer depends on org.springframework spring-context-indexer 5.2.0.RELEASE true

Although classpath scanning is very fast, there are a large number of classes within Spring, and adding this dependency can improve the startup performance of large applications by creating a static list of candidate objects at compile time.

In this mode, all modules that are the target of component scanning must use this mechanism.

Configure springboot to automatically open the browser spring-context-indexer principle int port= 8080 X 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 string + port) } catch (IOException e) {e.printStackTrace ();}

Now the springboot project in use is large, and each startup is very slow, so speed up startup with the following dependencies:

Spring-context-indexer

Description

1. Although classpath scanning is very fast, there are a large number of classes within Spring, and adding this dependency can improve the startup performance of large applications by creating a static list of candidate objects at compile time.

two。 However, in this mode, all modules that are the target of component scanning must use this mechanism. Only then.

3. You need more than spring5 to use it. The test is valid. The dependence is as follows:

Org.springframework spring-context-indexer 5.2.0.RELEASE true

Then add a note to your startup class: @ Indexed, which means that if you open the index, it will speed up the startup of the project.

Description of the principle:

Let's first look at the official explanation:

After @ Indexed is used in the project, the META-INT/spring.components file is automatically generated in the project when you compile the package.

When the Spring application context performs a ComponentScan scan, the META-INT/spring.components will be read and loaded by CandidateComponentsIndexLoader and converted into a CandidateComponentsIndex object, so @ ComponentScan will not scan the specified package, but read the CandidateComponentsIndex object, thus improving performance.

Pay attention to the use

Although this @ Indexed annotation can improve performance, you should also pay attention to it when using it.

Assuming that there is only pattern annotations in an a.jarreb. Jar containing META-INT/spring.components resources in the Spring application, the pattern annotations in the b.jar will not be recognized when scanning the package in both JAR with @ ComponentScan.

Please be sure to pay attention to such problems.

Thank you for reading! This is the end of this article on "what is the use of spring-context-indexer dependence in Spring?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Object performance pattern annotation project application compilation content principle can be passed application time is in more mechanism module goal program article component Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Docker MariaDB NVidia