[SpringBoot] Introduction and Traditional Spring Framework: Comparison and Analysis

 Hello, hello, everyone~ I am your old friend: protect Xiao Zhou ღ  


What I bring to you today is an introduction to SpringBoot. The creation of the SpringBoot project has advantages over the Spring framework: 1. Fast integration framework 2. Built-in running container, fast deployment project 3. Abandon cumbersome xml, use annotations and Configured to develop. 4. Support more monitoring indicators, this article will explain it to you, let’s take a look~


This issue is included in the blogger's column : JavaEE_Protect Xiao Zhouღ's Blog-CSDN Blog

Suitable for programming beginners, interested friends can subscribe to view other "JavaEE Basics".

Stay tuned for more highlights: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★*'

1. Introduction to SpringBoot

The origin of SpringBoot can be traced back to the Spring framework . Spring is a very popular open source Java framework for building enterprise applications. However, developing traditional Java applications using the Spring framework requires cumbersome configuration and additional manual steps for deployment.

In order to simplify the development and deployment process of Spring applications, SpringBoot was launched by the Pivotal team in 2013 ( not the same team as Spring ). Its goal is to make it easier for developers to create and deploy Spring applications by providing a series of features such as automatic configuration, fast startup, embedded server and starting dependencies.

SpringBoot's design philosophy is to improve development efficiency with a "contract greater than configuration" approach. It adopts a set of predefined rules and conventions, automatically configures according to the project's dependencies and class paths , and does not require manual configuration in most cases. Developers only need to focus on the implementation of business logic without wasting time on tedious configuration.

Spring was born to simplify the development of Java applications, and SpringBoot was born to simplify the development of Spring programs, and SpringBoot was born to quickly develop the Spring framework.

For example: Spring cuts rice

SpringBoot cutting rice

It is also a very sharp contrast. The update and iteration of technology, the new technology must have some improvement in efficiency compared with the previous technology.


2. Advantages of SpringBoot

The advantages are based on the Spring framework~

2.1 Fast integration framework:

If we want to create a Spring project , the most common way is to create a Maven project first, then obtain Spring dependencies from the Maven central warehouse (Spring belongs to a third-party open source framework), and inject them into the pom.xml configuration file.

After finishing everything, we may need to add some other third-party dependencies, such as: JDBC (database) , some tools rely on lombok , etc. I don’t know if careful friends have discovered it. The various dependencies we added have A version number. If these third-party dependencies are incompatible with each other, if we use these dependencies , the program may not start, or some functions may be paralyzed.

So to create a Spring project , we need to add related dependencies, and we need to pay attention to whether the versions of dependencies are compatible! ! !


 2.1.1 Creation of SpringBoot

If we use the community version of IDEA , we must first install the Spring Boot Helper plug-in (this plug-in is free until the community version 2022, and later versions need to be charged) to create a SpringBoot project.

 After installation, its name becomes Spring Initializr and Assistant:

 

 Then we can use this plugin to create when we use IDEA to create a project~

Pay attention to choose the version of JDK installed on your machine

After clicking next:

 Click Finish to complete the Spring Boot project creation.


We can also create a SpringBoot project without using IDEA, use the web version provided by Spring’s official website to create a SpringBoot project, or create a project with the web version provided by Ali

Web version creation path: https://start.spring.io
After the web version creates a project, you will get a SpringBoot zip package. After decompression, use Idea to open it, and you can use the project directly. Note that you need to select when creating a project on the web page The JDK adaptation is adapted to the local JDK version.


We use IDEA to create a SpringBoot project and feel that the process is very cumbersome, but these processes are all necessary, without a trace of muddy water. The most important thing is that SpringBoot can help us adapt to other third-party frameworks according to the SpringBoot version ( Dependency) version . This is his advantage, it can quickly integrate the framework.


2.2 Built-in running container, rapid deployment project

SpringBoot has a variety of built-in containers , and you can choose the appropriate container according to your needs. The following are the built-in containers of Spring Boot:

  1. Tomcat (default) : The default container used by Spring Boot is Apache Tomcat. Tomcat is a lightweight Java Web (HTTP) server, and a Servlet container. Spring Boot automatically configures Tomcat as the default built-in container.

  2. Jetty: Spring Boot also supports using Jetty as a built-in container. Jetty is another popular Java web server and Servlet container. You can switch to the Jetty container by adding a jetty dependency in the pom.xml file.

  3. Undertow: Another optional built-in container is Undertow. Undertow is a lightweight, high-performance web server developed by JBoss. You can switch to the Undertow container by adding an undertow dependency in the pom.xml file.

The built-in container used can be configured by setting the relevant properties in the Spring Boot configuration file (application.properties or application.yml). For example, you can set the server.port property to specify the port number to listen on.

It should be noted that SpringBoot also supports deploying applications through external containers, such as using a separate Tomcat or Jetty server to host applications. Doing so provides more flexible deployment and scaling options.

Java is the most used in running and deploying Tomcat, which is an HTTP protocol server written in Java code, and JavaWeb relies on these built-in containers to provide APIs to process HTTP protocol datagrams.

Like a Servlet project, we need to manually configure an external Tomcat and then package and deploy the project into the container. When deploying the project, we also need to install a Tomcat on the server, and the SpringBoot project integrates Tomcat, so we can quickly deploy the project and start it without an external container.


2.3 Abandon the cumbersome xml and use annotations and configurations for development.

When Spring starts, those classes in the package that need to be scanned according to the package path configured in the Spring configuration file (.xml) are annotated and annotated, and then these annotated class instances (Bean objects) are sent to the Spring container.

For the SpringBoot project, when we put the container class and the startup class to be injected into the same directory:

At this time, when the project is started, the annotated classes can be scanned and the Bean object can be injected into the container.

Comparing the Spring project, we can see the difference intuitively. In Spring, the package scanning path of the Bean object needs to be configured, but SpringBoot does not (the convention is greater than the configuration ). When we are working on a large project, there are many directories. It is a very cumbersome operation to configure paths with xml files.

Another advantage of SpringBoot is that there are configuration files and automatic assembly . This is not a concept with Spring automatic assembly. When the Boot project starts, you can read the third-party data required by the class instance from the configuration file! !

For example, create a Mysql database object - datasource

The usual practice is that we manually configure the data source:

This means that it is written to death, because we package and deploy the project and generate a bytecode file (.class binary). If our actual operating environment does not match the data in our project, the locally debugged database will be the same as that on the server The databases are two different . The user name and password are not necessarily the same ( it can run locally, but not on the server, and needs to be debugged ) , or because some data on the server needs to be modified (account, password), but for our bytecode file, If it cannot be modified, the program will naturally fail to run, and it has to be reworked. It is very troublesome for developers to readjust and then package and deploy.

When the SpringBoot project creates a Mysql data source, it first reads relevant information from the configuration file, and then generates a Mysql data source for us, which means that the file can be modified at any time, and the key data that the class depends on when it is an instance Can vary.

Including the port number, the local default is 8080. The server port 8080 in the production environment may be occupied. In this case, we can also modify the port number of the application in the configuration file. These are the advantages of the SpringBoot project.

Of course, in ordinary projects, we can also create a configuration file and use the IO stream to read relevant information. To put it bluntly, it increases the complexity of the class, and the code of the programmer needs to be written in a frame, but SpringBoot comes with it, and It can be read with annotations. The SpringBoot configuration file is very powerful, please listen to the next chapter for details~


2.4 Support more monitoring indicators

SpringBoot provides some built-in monitoring indicators, and more information can be obtained through integrated monitoring tools or custom indicators. The following are some examples of monitoring indicators supported by SpringBoot:

  1. Health Indicators (Health Indicators): Used to check the health of the application, including memory usage, database connection status, etc.

  2. Memory Metrics: Track the memory usage of the application, including heap memory usage, non-heap memory usage, etc.

  3. CPU Metrics: Monitors the processor usage of an application while processing requests.

  4. Thread Metrics: Track your application's thread pool usage, the number of active threads, etc.

  5. Request Metrics: Monitor the request processing of the application, such as the number of requests, average response time, etc.

  6. Database metrics (Database Metrics): Used to track the interaction between the application and the database, such as query execution time, connection pool usage, etc.

  7. Log Metrics: Track the log output of the application, such as log level, number of log entries, etc.

  8. Cache Metrics: Monitor application cache usage, such as cache hit rate, cache clear times, etc.

These indicators can be easily monitored and exposed through the Spring Actuator module, or you can choose to use the built-in monitoring tools or combine third-party monitoring tools for monitoring and analysis.

The most commonly used above is the log. The problems encountered during the running of the project can usually be reflected in the log. The content and level of the log can be determined by the programmer himself.

 I will also tell you about the related operations of the log in a later article...


Well, here we go, 【SpringBoot】Introduction and Traditional Spring Framework: Comparison and Analysis  Bloggers have finished sharing, I hope it will be helpful to everyone, and welcome criticism and correction if there is anything wrong. 

Next preview: [SpringBoot] log, [SpringBoot] configuration file

Thank you to everyone who read this article, and more exciting events are coming: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★* 

When I met you, all the stars fell on my head ...

Guess you like

Origin blog.csdn.net/weixin_67603503/article/details/131779588