1.概述:关于Spring和SpringBoot的区别,听过很多回答。刚开始学习SpringBoot的时候是一头雾水。随着经验的积累,我逐渐明白了这两个框架的区别。相信对于长期使用SpringBoot的开发者来说,大部分还是不明白SpringBoot与Spring的区别。看完文中的对比,或许你会有不同的答案和看法!2.什么是春天?先说说Spring。作为Java开发者,对于Spring大家都不陌生。简而言之,Spring框架为开发Java应用程序提供了全面的基础设施支持。它包含一些不错的功能,例如依赖注入和开箱即用的模块,例如:SpringJDBC、SpringMVC、SpringSecurity、SpringAOP、SpringORM、SpringTest。您应该使用过这些模块。这些模块缩短了应用程序的开发时间,提高了应用程序开发的效率。比如在JavaWeb开发初期,我们需要编写大量的代码来向数据源中插入记录。但是通过使用SpringJDBC模块的JDBCTemplate,我们可以将这个操作简化为几行配置。3.什么是SpringBoot?SpringBoot基本上是Spring框架的扩展,无需XML配置来设置Spring应用程序,为更快、更高效的开发生态系统铺平了道路。以下是SpringBoot的一些特点:1:创建一个独立的spring应用。2:嵌入Tomcat、JettyUndertow,不需要部署它们。3:提供“starters”poms,简化Maven配置4:尽可能自动配置spring应用。5:提供生产指标、健壮性检查和外部化配置6:绝对没有代码生成和XML配置要求4、让我们逐步熟悉这两个框架4.1、Maven依赖首先,让我们看看使用Spring创建一个web应用需要什么org.springframeworkspring-web5.1.0.RELEASEorg的最小依赖。springframeworkspring-webmvc5.1.0.RELEASE与Spring不同,SpringBoot只需要一个依赖项即可启动和运行Web应用程序:org.springframework.bootspring-boot-starter-web2.0.6.RELEASE在构建期间,所有其他依赖项将自动添加到项目中。另一个很好的例子是测试库。我们通常使用SpringTest、JUnit、Hamcrest和Mockito库。在Spring项目中,我们应该将所有这些库添加为依赖项。但是在SpringBoot中,我们只需要添加spring-boot-starter-test依赖就可以自动包含这些库。SpringBoot为不同的Spring模块提供了很多依赖。一些最常用的是:spring-boot-starter-data-jpaspring-boot-starter-securityspring-boot-starter-testspring-boot-starter-webspring-boot-starter-thymeleaf有关启动器的完整列表,请查看Spring文档。4.2.MVC配置让我们来看看Spring和SpringBoot创建一个JSPweb应用程序所需要的配置。Spring需要定义一个dispatcherservlet、映射和其他支持配置。我们可以使用web.xml文件或Initializer类来做到这一点:container.addServlet("dispatcher",newDispatcherServlet(context));dispatcher.setLoadOnStartup(1);dispatcher.addMapping("/");}}还需要在@Configuration类中添加@EnableWebMvc注解,定义一个视图解析器解析从控制器返回的视图:@EnableWebMvc@ConfigurationpublicclassClientWebConfigimplementsWebMvcConfigurer{@BeanpublicViewResolverviewResolver(){InternalResourceViewResolverbean=newInternalResourceViewResolver();bean.setViewClass(JstlView.class);bean.setPrefix("/WEB-INF/view/");bean。setSuffix(".jsp");returnbean;}}对比上面的操作,一次我们添加Webstarter,SpringBoot只需要在应用中在配置文件中配置几个属性来完成以上操作:spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp上面所有的Spring配置都是通过一个名为auto的过程-configuration添加Bootwebstarter自动包含这意味着SpringBoot将查看应用程序中存在的依赖项、属性和bean,并根据这些依赖项配置属性和bean。当然,如果我们想添加自己的自定义配置,那么SpringBoot自动配置将回退。4.3.配置模板引擎现在让我们看看如何在Spring和SpringBoot中配置Thymeleaf模板引擎。在Spring中,我们需要为视图解析器添加thymeleaf-spring5依赖项和一些配置:@Configuration@EnableWebMvcpublicclassMvcWebConfigimplementsWebMvcConfigurer{@AutowiredprivateApplicationContextapplicationContext;@BeanpublicSpringResourceTemplateResolvertemplateResolver(){SpringResourceTemplateResolvertemplateResolver=newSpringResourceTemplateResolver();templateResolver.setApplicationContext(applicationContext);templateResolver.setPrefix("/WEB-INF/views/");templateResolver.setSuffix(".html");returntemplateResolver;}@BeanpublicSpringTemplateEnginetemplateEngine(){SpringTemplateEnginetemplateEngine=newSpringTemplateEngine();templateEngine.setTemplateResolver(templateResolver());templateEngine.setEnableSpringELCompiler(true);returntemplateEngine;}@OverridepublicvoidconfigureViewResolvers(ViewResolverRegistryregistry){ThymeleafViewResolverresolver=newThymeleafViewResolver();resolver.setTemplateEngine(templateEngine());registry.viewResolver(解析器);}}SpringBoot1X在Web应用程序中启用Thymeleaf支持只需要spring-boot-starter-thymeleaf的依赖项,但由于Thymeleaf3.0中的新功能,我们必须在SpringBoot2XWeb应用程序中添加thymeleaf-layout-dialect作为依赖项。一旦依赖关系到位,我们就可以将模板添加到src/main/resources/templates文件夹中,SpringBoot将自动显示它们。4.4、SpringSecurity配置为了简单起见,我们使用框架默认的HTTPBasic认证。我们先来看看启用SecuritywithSpring所需的依赖和配置。Spring首先需要依赖spring-security-web和spring-security-config模块。接下来,我们需要添加一个扩展WebSecurityConfigurerAdapter的类,并使用@EnableWebSecurity注解:@Configuration@EnableWebSecuritypublicclassCustomWebSecurityConfigurerAdapterextendsWebSecurityConfigurerAdapter{@AutowiredpublicvoidconfigureGlobal(AuthenticationManagerBuilderauth)throwsException{auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("password")).authorities("ROLE_ADMIN");}@Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http.authorizeRequests().anyRequest().authenticated().and().httpBasic();}@BeanpublicPasswordEncoderpasswordEncoder(){returnnewBCryptPasswordEncoder();}}这里我们使用inMemoryAuthentication来设置身份验证。同样,SpringBoot也需要这些依赖项才能正常工作。但我们只需要定义spring-boot-starter-security的依赖,因为这会自动将所有相关依赖添加到类路径中。SpringBoot中的安全配置同上。5.应用程序引导配置Spring和SpringBoot应用程序引导的基本区别在于servlet。Spring使用web.xml或SpringServletContainerInitializer作为其引导入口点。SpringBoot仅使用Servlet3功能来引导应用程序。让我们仔细看看5.1。Spring是如何引导配置的?Spring支持传统的web.xml引导方法以及最新的Servlet3+方法。我们看一下web.xml方法的步骤:Servlet容器(服务端)读取web.xml中定义的DispatcherServletweb.xml容器实例化DispatcherServlet通过读取WEB-INF/{servletName}-servlet.xml*创建WebApplicationContext***,DispatcherServlet注册在应用程序上下文中定义的beans以下是使用Servlet3+方法的SpringBoot:容器搜索实现ServletContainerInitializer的类并执行SpringServletContainerInitializer以查找所有使用先前创建的上下文实现WebApplicationInitializer的类。5.2.SpringBoot是怎么配置的?SpringBoot应用程序的入口点是一个用@SpringBootApplication注释的类:@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}默认情况下,SpringBoot使用嵌入式容器来运行应用程序。在这种情况下,SpringBoot使用publicstaticvoid主入口点来启动嵌入式Web服务器。此外,它还负责将应用程序上下文中的Servlet、Filter和ServletContextInitializerbean绑定到嵌入式servlet容器。SpringBoot的另一个特点是它会自动扫描同一个包中的所有类或Main类的子包中的组件。SpringBoot提供了一种将其部署到外部容器的方法。在这种情况下我们必须扩展SpringBootServletInitializer:onStartup(servletContext);servletContext.addListener(newHttpSessionEventPublisher());}}这里外部servlet容器寻找war包-class下的META-INF文件夹下的MANIFEST.MF文件中定义的Main,SpringBootServletInitializer会负责绑定Servlet、Filter和ServletContextInitializer。6.打包部署***让我们看看如何打包和部署应用程序。这两个框架都支持常见的包管理技术,如Maven和Gradle。但在部署方面,这些框架差异很大。例如,SpringBootMaven插件在Maven中提供了SpringBoot支持。它还允许打??包可执行的jar或war包并就地运行应用程序。SpringBoot在部署环境上相对于Spring的一些优势包括:提供嵌入式容器支持使用命令java-jar独立运行jar在外部容器中部署时,可以选择排除依赖以避免潜在的jar冲突并灵活指定配置文件在部署过程中为集成测试生成随机端口的选项7.结论简而言之,我们可以说SpringBoot只是Spring本身的扩展,使开发、测试和部署更加方便。