当前位置: 首页 > 科技观察

SpringvsSpringBoot:3个核心区别

时间:2023-03-19 00:21:49 科技观察

概述关于Spring和SpringBoot的区别,我听过很多回答。刚开始学习SpringBoot的时候也是一头雾水。慢慢了解这两个框架的区别。相信对于长期使用SpringBoot的同学来说,对于SpringBoot和Spring的区别还是不太了解。看完文章中的对比,你可能会有不同的答案和看法!什么是Spring作为Java开发者,对于Spring大家都不陌生。简而言之,Spring框架为开发Java应用程序提供了全面的基础设施支持。它包含一些很好的特性,如依赖注入和开箱即用的模块,如:SpringJDBC、SpringMVC、SpringSecurity、SpringAOP、SpringORM、SpringTest,这些模块缩短了应用程序开发时间并提高了应用程序开发效率例如,在早期阶段JavaWeb开发中,我们需要写很多代码来向数据库中插入记录。但是通过使用SpringJDBC模块的JDBCTemplate,我们可以将操作简化为几行代码。什么是SpringBootSpringBoot基本上是Spring框架的扩展,它消除了设置Spring应用程序所需的XML配置,为更快、更高效的开发生态系统铺平了道路。SpringBoot的一些特点:1.创建一个独立的Spring应用。2、内嵌Tomcat、Jetty、Undertow容器(无需部署war文件)。3.提供的启动器简化构建配置4.尽可能自动配置spring应用。5.提供生产指标,例如指标、健康检查和外部化配置6.完全没有代码生成和XML配置要求从配置分析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文档。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;}}让我们再看看SpringBoot。一旦我们ddWeb启动器,SpringBoot只需要在在应用配置文件中配置几个属性来完成以上操作:spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp上面所有的Spring配置都是通过一个名为auto-的配置过程将Bootwebstarter添加到自动包含中,这意味着SpringBoot会查看应用程序中存在的依赖项、属性和bean,并根据这些依赖项配置属性和bean。当然,如果我们想添加自己的自定义配置,那么SpringBoot的自动配置就会回退。配置模板引擎现在让我们看看如何在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只需要依赖spring-boot-starter-thymeleaf即可在Web应用程序中启用Thymeleaf支持但由于Thymeleaf3.0中的新功能,我们必须将thymeleaf-layout-dialect添加为SpringBoot2XWeb应用程序中的依赖项。配置依赖后,我们可以将模板添加到src/main/resources/templates文件夹中,SpringBoot会自动显示出来。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中的安全配置同上。Spring和SpringBoot中应用程序引导的基本区别在于servlet。Spring使用web.xml或SpringServletContainerInitializer作为其引导入口点。SpringBoot仅使用Servlet3函数来引导应用。让我们仔细看看Springboot配置。Spring支持传统的web.xml启动方式和最新的Servlet3+方式。配置web.xml方法的步骤启动Servlet容器(服务器)读取web.xmlweb.xml中定义的DispatcherServlet实例化容器DispatcherServlet通过读取WEB-INF/{servletName}-servlet.xml创建WebApplicationContext。最后,DispatcherServlet使用Servlet3+方法注册在应用程序上下文中定义的bean,SpringBoot引导容器搜索实现ServletContainerInitializer的类并执行SpringServletContainerInitializer以查找所有实现WebApplicationInitializer``上下文的类。SpringBootBoot配置SpringBoot应用程序的入口点是一个用@SpringBootApplication注解的类@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}默认情况下,SpringBoot使用嵌入式容器运行应用程序。在这种情况下,SpringBoot使用publicstaticvoidmain入口点来启动嵌入式Web服务器。此外,它还负责将应用程序上下文中的Servlet、Filter和ServletContextInitializerbean绑定到嵌入式servlet容器。SpringBoot的另一个特点是它会自动扫描同一个包中的所有类或Main类的子包中的组件。SpringBoot提供了一种部署到外部容器的方法。我们只需要扩展SpringBootServletInitializer即可:returnapplication.sources(Application.class);}@OverridepublicvoidonStartup(ServletContextservletContext)throwsServletException{super.onStartup(servletContext);servletContext.addListener(newHttpSessionEventPublisher());}}此处外部servlet容器查找MANIFEST中定义的主类war包下的META-INF文件夹下的.MF文件,SpringBootServletInitializer会负责绑定Servlet、Filter和ServletContextInitializer。打包和部署最后,让我们看看如何打包和部署应用程序。这两个框架都支持常见的包管理技术,如Maven和Gradle。但在部署方面,这些框架差异很大。例如,SpringBootMaven插件在Maven中提供了SpringBoot支持。它还允许打??包可执行的jar或war并就地运行应用程序。SpringBoot在部署环境上相对于Spring的一些优势包括:1.提供嵌入式容器支持2.使用命令java-jar独立运行jar3。在外部容器中部署时,可以选择排除依赖,避免潜在的jar冲突4.部署时灵活指定配置文件的选项5.集成测试的随机端口生成结论总之,我们可以说SpringBoot只是对Spring本身,让开发、测试、部署更加方便。