In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces what are the three core differences between Spring and SpringBoot. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Overview
I heard a lot of answers about the difference between Spring and SpringBoot, and I was confused when I first started to learn SpringBoot. With the accumulation of experience, I slowly understand the difference between the two frameworks. I believe that for students who have used SpringBoot for a long time, I do not quite understand the difference between SpringBoot and Spring.
What is Spring?
As Java developers, everyone is familiar with Spring. In short, the Spring framework provides comprehensive infrastructure support for the development of 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 application development time and improve the efficiency of application development. For example, in the early stage 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, eliminating the XML configuration required to set up Spring applications, paving the way for a faster and more efficient development ecosystem.
Some features in SpringBoot:
1. Create independent Spring applications.
2. Embedded Tomcat, Jetty, Undertow containers (no need to deploy war files).
3. The starters provided to simplify the construction configuration
4. Configure spring applications automatically as much as possible.
5. Provide production indicators, such as indicators, robustness check and externalized configuration
6. There are no code generation and XML configuration requirements at all
From configuration analysis
Maven dependence
First, let's look at the minimum dependencies required to create a Web application using Spring
Org.springframework spring-web 5.1.0.RELEASE org.springframework spring-webmvc 5.1.0.RELEASE
Unlike Spring, Spring Boot requires only one dependency to start and run Web applications:
Org.springframework.boot spring-boot-starter-web 2.0.6.RELEASE
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 of these libraries as dependencies. But in SpringBoot, we just need to add spring-boot-starter-test dependencies to automatically include these libraries.
Spring Boot provides many dependencies for different Spring modules. Some of the most common ones are:
Spring-boot-starter-data-jpaspring-boot-starter-securityspring-boot-starter-testspring-boot-starter-webspring-boot-starter-thymeleaf
For a complete list of starter, check the Spring documentation.
MVC configuration
Let's take a look at the configuration required by Spring and SpringBoot to create a JSPWeb application.
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:
Public class MyWebAppInitializer implements WebApplicationInitializer {@ Override public void onStartup (ServletContext container) {AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext (); context.setConfigLocation ("com.pingfangushi"); container.addListener (new ContextLoaderListener (context)); ServletRegistration.Dynamic dispatcher = container .addServlet ("dispatcher", new DispatcherServlet (context)); dispatcher.setLoadOnStartup (1) Dispatcher.addMapping ("/");}}
You also need to add the @ EnableWebMvc annotation to the @ Configuration class and define a view parser to parse the view returned from the controller:
@ EnableWebMvc @ Configuration public class ClientWebConfig implements WebMvcConfigurer {@ Bean public ViewResolver viewResolver () {InternalResourceViewResolver bean = new InternalResourceViewResolver (); bean.setViewClass (JstlView.class); bean.setPrefix ("/ WEB-INF/view/"); bean.setSuffix (".jsp"); return bean;}}
Let's take a look at SpringBoot once we add the Web launcher, SpringBoot only needs to configure a few properties in the application configuration file to do this:
Spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
All of the above Spring configurations are automatically included through a process called auto-configuration by adding Bootweb starter.
This means that SpringBoot will look at the dependencies, properties, and bean that exist in the application, and configure the properties and bean based on these dependencies. Of course, if we want to add our own custom configuration, then the SpringBoot auto configuration will be returned.
Configure template engine
Now let's look at how to configure the Thymeleaf template engine in Spring and Spring Boot.
In Spring, we need to add thymeleaf-spring5 dependencies and some configuration to the view parser:
@ Configuration @ EnableWebMvc public class MvcWebConfig implements WebMvcConfigurer {@ Autowired private ApplicationContext applicationContext; @ Bean public SpringResourceTemplateResolver templateResolver () {SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver (); templateResolver.setApplicationContext (applicationContext); templateResolver.setPrefix ("/ WEB-INF/views/"); templateResolver.setSuffix (".html"); return templateResolver } @ Bean public SpringTemplateEngine templateEngine () {SpringTemplateEngine templateEngine = new SpringTemplateEngine (); templateEngine.setTemplateResolver (templateResolver ()); templateEngine.setEnableSpringELCompiler (true); return templateEngine;} @ Override public void configureViewResolvers (ViewResolverRegistry registry) {ThymeleafViewResolver resolver = new ThymeleafViewResolver () Resolver.setTemplateEngine (templateEngine ()); registry.viewResolver (resolver);}}
SpringBoot1X only needs spring-boot-starter-thymeleaf dependencies to enable Thymeleaf support in Web applications. but because of the new functionality in Thymeleaf3.0, we have to add thymeleaf-layout-dialect as a dependency in the SpringBoot2XWeb application. Once the dependencies are configured, we can add 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 configurations required to enable Security using Spring.
Spring first needs to rely on spring-security-web and spring-security-config modules. Next, we need to add a class that extends WebSecurityConfigurerAdapter and annotate it with @ EnableWebSecurity:
@ Configuration @ EnableWebSecurity public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {@ Autowired public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication () .withUser ("admin") .password (passwordEncoder () .encode ("password")) .authorities ("ROLE_ADMIN") } @ Override protected void configure (HttpSecurity http) throws Exception {http.authorizeRequests () .anyRequest () .authenticated () .and () .httpBasic ();} @ Bean public PasswordEncoder passwordEncoder () {return new BCryptPasswordEncoder ();}}
Here we use inMemoryAuthentication to set up authentication. Similarly, SpringBoot needs these dependencies to make it work. But we only need to define the dependencies for spring-boot-starter-security, because this automatically adds all related dependencies to the classpath.
The security configuration in SpringBoot is the same as above.
The application starts the boot configuration
The basic difference between application bootstrapping in Spring and SpringBoot is servlet. Spring uses web.xml or SpringServletContainerInitializer as its boot entry point. SpringBoot only uses the Servlet3 feature to boot the application, so let's take a closer look at it.
Spring Boot configuration
Spring supports traditional web.xml boot methods as well as the latest Servlet3+ methods.
To configure the startup of the web.xml method
Servlet container (server) reads web.xml
The DispatcherServlet defined in web.xml is instantiated by the container
DispatcherServlet creates the WebApplicationContext by reading WEB-INF/ {servletName}-servlet.xml. Finally, DispatcherServlet registers the bean defined in the application context
Use the Spring startup steps of the Servlet3+ method
The container searches for the class that implements ServletContainerInitializer and executes SpringServletContainerInitializer to find the implementation of all classes WebApplicationInitializer to create a context with XML or the @ configuration class WebApplicationInitializer to create the DispatcherServlet with the previously created context.
SpringBoot Boot configuration
The entry point for a SpringBoot application is a class annotated with @ SpringBootApplication
@ SpringBootApplication public class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
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 responsible for binding 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 subpackages of the Main class.
SpringBoot provides a way to deploy it to an external container. All we need to do is extend SpringBootServletInitializer:
/ * * War deployment * * @ author SanLi * Created by 2689170096@qq.com on 2018-4-15 * / public class ServletInitializer extends SpringBootServletInitializer {@ Override protected SpringApplicationBuilder configure (SpringApplicationBuilder application) {return application.sources (Application.class);} @ Override public void onStartup (ServletContext servletContext) throws ServletException {super.onStartup (servletContext) ServletContext.addListener (new HttpSessionEventPublisher ());}}
Here the external servlet container looks for the Main-class defined in the MANIFEST.MF file under the META-INF folder under the war package, and SpringBootServletInitializer will be responsible for binding Servlet, Filter and ServletContextInitializer.
Packaging and deployment
Finally, let's look at how to package and deploy the application. Both frameworks support general package management technologies such as Maven and Gradle. But in terms of deployment, these frameworks vary widely. For example, the SpringBoot Maven plug-in provides SpringBoot support in Maven. It also allows you to package executable jar or war packages and run applications in place.
Some of the advantages of SpringBoot over Spring in a deployment environment include:
1. Provide embedded container support
2. Use the command java-jar to run jar independently
3. When deploying in an external container, you can choose to exclude dependencies to avoid potential jar conflicts
4. Flexible options for specifying configuration files at deployment time
5. Random port generation for integration testing
In short, we can say that SpringBoot is just an extension of Spring itself, making it easier to develop, test, and deploy.
On the three core differences between Spring and SpringBoot what is shared here, I hope the above content can be of some help to you, can 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.