Share and share your own learning materials, friends in need can find me to get
According to their own interview experience and constantly collected (collector's edition)
[Recommended] The latest collection of Java e-books in 2020.pdf (Hematemesis) >>>
https://www.cnblogs.com/xiaogeng88/p/12692306.html
I believe that many small partners, like me, often use Spring and Spring Boot, but just did not study the difference between the two?
Come to the big secret today ↓
Overview
As for the difference between Spring and SpringBoot, I heard many answers. When I first started learning SpringBoot, I was also confused. With the accumulation of experience, I slowly understood the difference between these two frameworks. Believe For the students who have used Spring Boot for a long time, they still don't understand the difference between Spring Boot and Spring. After reading the comparison in the article, maybe you have different answers and opinions!
What is Spring
As Java developers, everyone is familiar with Spring. In short, the Spring framework provides comprehensive infrastructure support for developing Java applications. It contains some good features, such as dependency injection and out-of-the-box modules, such as: SpringJDBC, SpringMVC, SpringSecurity, SpringAOP, SpringORM, SpringTest. These modules shorten the development time of applications and improve the efficiency of application development. For example, In the early stages of JavaWeb development, we need to write a lot of code to insert records into the database. But by using the JDBCTemplate of the SpringJDBC module, we can simplify the operation to a few lines of code.
What is Spring Boot
SpringBoot is basically an extension of the Spring framework, which eliminates the XML configuration required to set up Spring applications, paving the way for a faster and more efficient development ecosystem.
SpringBoot中的一些特征:
1、创建独立的 Spring应用。
2、嵌入式 Tomcat、 Jetty、 Undertow容器(无需部署war文件)。 3、提供的 starters 简化构建配置 4、尽可能自动配置 spring应用。 5、提供生产指标,例如指标、健壮检查和外部化配置 6、完全没有代码生成和 XML配置要求
From configuration analysis
Maven dependency
First, let's take a look at the minimum dependencies required to create a web application using Spring
Unlike Spring, Spring Boot only needs one dependency to start and run the web application:
During the build, all other dependencies are automatically added to the project.
Another good example is the test library. We usually use SpringTest, JUnit, Hamcrest and Mockito libraries. In the Spring project, we should add all these libraries as dependencies. But in SpringBoot, we only need to add the spring-boot-starter-test dependency to automatically include these libraries.
Spring Boot为不同的Spring模块提供了许多依赖项。一些最常用的是:
spring-boot-starter-data-jpaspring-boot-starter-securityspring-boot-
starter-testspring-boot-starter-webspring-boot-starter-thymeleaf
有关 starter的完整列表,请查看Spring文档。
MVC configuration
Let's take a look at the configuration required by Spring and SpringBoot to create JSPWeb applications.
Spring needs to define the scheduler servlet, mapping and other supporting configurations. We can use the web.xml file or the Initializer class to do this:
You also need to add the @EnableWebMvc annotation to the @Configuration class and define a view resolver to parse the view returned from the controller
Let's take a look at SpringBoot. Once we added the Web startup program, SpringBoot only needs to configure a few properties in the application configuration file to complete the above operations:
All the Spring configuration above is automatically included by adding a Bootweb starter through a process called auto-configuration.
This means SpringBoot will look at the dependencies, properties and beans that exist in the application and configure the properties and beans according to these dependencies. Of course, if we want to add our own custom configuration, then SpringBoot automatic configuration will be returned.
Configure template engine
Now let's see how to configure Thymeleaf template engine in Spring and Spring Boot.
In Spring, we need to add thymeleaf-spring5 dependency and some configuration for the view resolver:
SpringBoot1X only requires the dependency of spring-boot-starter-thymeleaf to enable Thymeleaf support in web applications. But due to the new features in Thymeleaf 3.0, we must add thymeleaf-layout-dialect as a dependency in the SpringBoot2XWeb application. After configuring the dependencies, we can add the templates to the src / main / resources / templates folder, and SpringBoot will automatically display them.
Spring Security configuration
For simplicity, we use the framework's default HTTPBasic authentication. Let's first look at the dependencies and configuration required to enable Security using Spring.
Spring first needs to rely on the spring-security-web and spring-security-config modules. Next, we need to add a class that extends WebSecurityConfigurerAdapter and annotate it with @EnableWebSecurity:
Here we use inMemoryAuthentication to set up authentication. Similarly, Spring Boot needs these dependencies to make it work. But we only need to define the dependency of spring-boot-starter-security, because this will automatically add all related dependencies to the classpath.
The security configuration in SpringBoot is the same as above.
Application startup boot configuration
The basic difference between application guidance in Spring and Spring Boot is the servlet. Spring uses web.xml or SpringServletContainerInitializer as its boot entry point. SpringBoot only uses the Servlet3 function to guide the application, let us understand in detail below
Spring boot configuration
Spring supports the traditional web.xml boot method and the latest Servlet3 + method.
Steps to configure web.xml method startup
Servlet container (server) reads web.xml
DispatcherServlet defined in web.xml is instantiated by the container
DispatcherServlet creates WebApplicationContext by reading WEB-INF / {servletName} -servlet.xml. Finally, DispatcherServlet registers the beans defined in the application context
Spring startup steps using Servlet3 + method
The container searches for classes that implement ServletContainerInitializer and executes SpringServletContainerInitializer to find all classes that implement WebApplicationInitializer
WebApplicationInitializer creates a DispatcherServlet with XML or context @Configuration class WebApplicationInitializer creates a previously created context.
SpringBoot boot configuration
The entry point of a Spring Boot application is a class annotated with @SpringBootApplication
By default, SpringBoot uses embedded containers to run applications. In this case, SpringBoot uses the publicstaticvoidmain entry point to start the embedded Web server. In addition, it is also responsible for binding the Servlet, Filter and ServletContextInitializerbean from the application context to the embedded servlet container. Another feature of SpringBoot is that it automatically scans all classes in the same package or components in sub-packages of the Main class.
SpringBoot provides a way to deploy it to external containers. We only need to extend SpringBootServletInitializer:
Here the external servlet container looks for the Main-class defined in the MANIFEST.MF file in the META-INF folder under the war package. SpringBootServletInitializer will be responsible for binding the Servlet, Filter and ServletContextInitializer.
Package and deploy
Foreword: Packaging and deployment actually involves not much, but there is also a simple understanding
Finally, let's see how to package and deploy the application. Both frameworks support common package management technologies such as Maven and Gradle. But in terms of deployment, these frameworks are very different. For example, the Spring Boot Maven plugin provides Spring Boot support in Maven. It also allows packaging executable jar or war packages and running applications on-site.
在部署环境中 SpringBoot 对比 Spring的一些优点包括:
1、提供嵌入式容器支持 2、使用命令java -jar独立运行jar 3、在外部容器中部署时,可以选择排除依赖关系以避免潜在的jar冲突 4、部署时灵活指定配置文件的选项 5、用于集成测试的随机端口生成
in conclusion
In short, we can say that SpringBoot is just an extension of Spring itself, making development, testing and deployment more convenient.