In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the solution to the sudden slow start of the springboot project, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
The springboot project suddenly started slowly.
The springboot project was originally running very fast in debug mode, but then one day, half of the project suddenly got stuck at that point. I thought it was my computer problem, or the code problem I wrote. Later, I searched the Internet and combined with my own project situation, it turned out to be a breakpoint problem.
There's a breakpoint that can't be removed anyway. It may have been left over before, and the code has been deleted.
It may also be because the code in this place belongs to what node to load and run, anyway, it can't be removed.
Later, according to the online method, under the debug mode window, select the Run menu and click the option of Remove All Breakpoints (it seems that you can also choose Skip All Breakpoing. )
Then all the breakpoints are removed, restart, smooth!
Springboot starts too slowly to optimize
Next, let's discuss each problem together.
1. Problems caused by automatic scanning of components (@ SpringBootApplication)
As we explained in our first blog post, by default, we use the @ SpringBootApplication annotation to automatically get the configuration information of the application, but this also has some side effects. Using this annotation triggers automatic configuration (auto-configuration) and component scanning (component scanning), which is the same as using @ Configuration, @ EnableAutoConfiguration, and @ ComponentScan annotations. While this brings convenience to development, it will have the following effects:
(a) it takes longer to start the project (because it wastes cpu and memory resources by loading components that we don't need to use). The impact is particularly pronounced when starting a large application, or when a large number of integration tests will be done to start the application.
(B) some unnecessary extra instances (beans) will be loaded.
(C) will increase CPU consumption and memory footprint.
two。 How to avoid problems caused by automatic scanning of components (do not use @ SpringBootApplication)
In the mentality that there are problems to be solved, in view of the above problems, how can we solve them? Obviously, since @ SpringBootApplication loads some unnecessary configurations, we wonder if we can just load our own specified configurations. Our idea does not use @ SpringBootApplication and does not use @ ComponentScan annotation (this annotation automatically scans the classes annotated by us with @ Controller,@Service and loaded into the Spring IOC container), and then we use @ Configuration and @ EnableAutoConfiguration to configure the startup class as follows:
Package com.kfit.spring_boot_performance; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; import com.kfit.spring_boot_performance.controller.HelloController / * * @ author Angel-Guardian Angel * @ version v.0.1 * @ date March 11, 2017 * / / remove @ SpringBootApplication and @ ComponentScan, and replace @ Configuration@EnableAutoConfigurationpublic class App {public static void main (String [] args) {SpringApplication.run (App.class, args);}} 3 with @ EnableAutoConfiguration. Problem caused-unable to scan component
We were about to rejoice in our code improvements when we found a problem. After startup, visit the access page / index that we wrote
Error: There was an unexpected error (type=Not Found, status=404).
What caused this? Remember the @ ComponentScan annotation we just introduced, enabling this annotation Spring to scan components automatically, otherwise you won't be able to scan the component classes we wrote. So the question is, what should we do? The solution to the problem is to configure explicitly.
The injection code is as follows (suppose the class we write is HelloController, where the blogger writes directly to the App.java startup class for injection):
@ Bean public HelloController helloController () {return new HelloController ();}
Explicitly configure it explicitly with the @ Bean annotation in the above code so that it can be scanned by Spring.
After restarting, we can visit the / index page normally.
At this point, someone will certainly say: in that case, it will not increase the amount of our coding. All I can say is: you have to load quickly and not encode. The blogger really doesn't know what to do. Everything has its pros and cons, weigh the pros and cons.
4. Through the ages, the Red Mansion is only a dream.
Some people don't believe that this can really start faster, so they test the code. Haha, it's exposed, but it still starts as slow as a snail. Then why is that? Why do we study for a long time, but in the end: the Red Mansion is only a dream, the bamboo basket is in vain.
Smart readers will notice that we mentioned that the @ SpringBootApplication annotation is equivalent to the @ EnableAutoConfiguration annotation, which means that it can also cause the above problems. To avoid these problems, we need to know what our component list is.
5.debug debug,bug bug is healthier
As we said above, our question is how do we know what our component list is? Then debug made his grand debut and applauded Mr. debug for his appearance.
Mr. debug: do you have any acceptance speech at this moment?
Mr. debug: after a slow life, I finally found my worth. Here I would like to thank CCTV, MTV, Coca-Cola, very Coke, Jia Duobao, Wang Laoji and SpringBoot, for giving me the opportunity to meet you on this stage. Thank you. I won't let you down.
All right, enough nonsense, let's take a look at how to use debug first.
The first case: use spring-boot:run startup mode
In this case, the complete running code is:
Spring-boot:run-Ddebug
The second case: use Run As-Java Application startup mode
In this case, you can configure the VM parameter as follows:
[right]-- [Run As]-- [Run Configurations... ]-- [Select Arguments]-- [VM arguments] add: [- Ddebug].
At this point, when we start up, we can see that the console prints out some log information that we don't normally see.
=
AUTO-CONFIGURATION REPORT
=
Positive matches:
-
DispatcherServletAutoConfiguration matched
-@ ConditionalOnClass found required class' org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
-@ ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)
/ / the remaining print information is omitted here.
6. Analyze Positive matches and Negative matches
In the print message, we need to know some knowledge here first:
(a) Positive match: accumulate configuration items that match to the corresponding class.
(B) Negative match: the reason for not including a configuration item.
Now let's take DataSourceAutoConfiguration as an example:
(a) @ ConditionalOnClass means that the corresponding configuration file will be parsed only when the corresponding class exists in the classpath directory. For DataSourceAutoConfiguration, it means that the corresponding database resources will be configured only when both javax.sql.DataSource and org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType classes exist.
(B) @ ConditionalOnMissingClass indicates that the corresponding class cannot be found in the classpath directory.
(C) OnClassCondition is used to indicate the type of match (postive or negative).
OnClassCondition is the most common browse detection condition, in addition, Spring Boot also uses other detection conditions, such as: OnBeanCondition is used to detect the existence of the specified bean instance, OnPropertyCondition is used to check the existence of the specified attribute, and so on.
Negative match conformance represents configuration classes (xxxConfiguration and the like) that exist in the classpath directory, but the other classes that depend on the annotations that decorate them do not exist.
7. Optimize configuration information again
According to the above theory, we only need to explicitly introduce these components at startup and copy the information listed in Positive matches:
DispatcherServletAutoConfiguration EmbeddedServletContainerAutoConfiguration ErrorMvcAutoConfiguration HttpEncodingAutoConfiguration HttpMessageConvertersAutoConfiguration JacksonAutoConfiguration JmxAutoConfiguration MultipartAutoConfiguration ServerPropertiesAutoConfiguration PropertyPlaceholderAutoConfiguration ThymeleafAutoConfiguration WebMvcAutoConfiguration WebSocketAutoConfiguration
Then update the project configuration, explicitly introduce these components, and then run the application to make sure there are no errors:
@ Configuration@Import ({DispatcherServletAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class, ErrorMvcAutoConfiguration.class, HttpEncodingAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, JacksonAutoConfiguration.class, JmxAutoConfiguration.class, MultipartAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, ThymeleafAutoConfiguration.class, WebMvcAutoConfiguration.class, WebSocketAutoConfiguration.class,}) public class App {
In the above code, we can delete the component information we do not need to improve the performance of the application, for example, if we do not use Jmx and WebSocket functions in the project, then we can delete JmxAutoConfiguration.class and WebSocketAutoConfiguration.class.
After you delete it, run the project again to make sure everything is all right.
The main idea to speed up the fast startup of spring boot is to discard @ Spring Boot Application and explicitly introduce the components we need.
So much for the solution to the sudden slow start of the springboot project. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.