Stack Length shared a fun framework the other day: a Java framework that is 44 times faster than Spring Boot! Does Spring Boot feel a little slow? Today, WE will talk about the new feature added to Spring Boot, which can greatly improve the speed of Spring Boot.

Recently, the Spring team announced an important addition to Spring Boot 2.2+ : lazy loading. It’s still a snapshot release, but let’s take a look at how to use this lazy loading feature.

What does lazy loading mean?

Lazy loading is already supported in the Spring framework. In short, it is the instantiation of a class. It does not need to be instantiated when the Spring container is started, but it is instantiated when it is first needed. It also saves system resources in certain procedures.

How do I enable lazy loading?

Here’s what we do in a traditional Spring project:

<bean id="testBean" calss="cn.javastack.TestBean" lazy-init="true" />
Copy the code

Is the above bean configuration familiar?

That’s right, lazy-init=”true” to indicate lazy loading, default is not false to indicate loading immediately upon container startup.

You can also do this after Spring 3.0+ :

@Lazy
public TestBean testBean() {
	return new TestBean();
}
Copy the code

@lazy: The default value is true, indicating Lazy loading.

How do I enable Spring Boot?

As you can see from the example above, lazy loading of beans is supported in any Spring Boot version, but this requires manual configuration, which can be cumbersome.

In Spring Boot 2.2+, deferred loading becomes easier, and there are several configurations:

  • Parameters: spring. Main. Lazy initialization
  • Class: SpringApplication
  • Class: SpringApplicationBuilder

By setting this to true, the Bean in the container is configured to be lazy-loaded.

Spring Boot project in IDE with DevTools, can make the development environment to start faster, 400ms can start up, greatly improve the development efficiency.

Are there any downsides to lazy loading?

Lazy loading can greatly reduce application startup time and save system resources, so the question, you might ask, is why not turn it on by default? Why provide an additional configuration?

Lazy loading does have many benefits, but it can also cause problems that can be found at startup but not until lazy loading, such as running out of memory, missing classes, or a host of problems caused by configuration errors.

Another problem is that because instantiation is not done until the first request is made, the first request may be slow, the response may be delayed, and the experience may not be great. This also has a negative impact on load balancing and automatic scaling.

conclusion

As we’ve analyzed above, lazy loading can significantly improve startup time, but it also has some significant downsides, so we should be careful to enable it. Or we can evaluate the project. Is lazy loading really that important or urgent for our project?

The official version of Spring Boot 2.2 will be released. Welcome to follow the official wechat account of Spring Boot 2.2: Java Technology stack.

Well, today’s share here, pay attention to the Java technology stack wechat public number, in the background reply: boot, get stack length arrangement of more Spring Boot tutorial, are actual practice dry things, the following is only part of the preview.

  • Several ways for Spring Boot to read configurations
  • How to verify Spring Boot parameters?
  • Spring Boot most core 25 notes!
  • Spring Boot 2.x startup process source code analysis
  • Spring Boot 2.x new features summary and migration guide

Finally, how do you use lazy loading

Java technology stack, has updated a lot of tutorials, this tutorial will continue to update…