概述
对于Spring和SpringBoot
到底有什么区别,我听到了很多答案,刚开始迈入学习
的我当时也是一头雾水,随着经验的积累、我慢慢理解了这两个框架到底有什么区别,相信对于用了
很久的同学来说,还不是很理解
到底和
有什么区别,看完文章中的比较,或许你有了不同的答案和看法!
什么是Spring
作为Java开发人员,大家都都不陌生,简而言之,框架为开发应用程序提供了全面的基础架构支持。它包含一些很好的功能,如依赖注入和开箱即用的模块,如:
SpringJDBC、SpringMVC、SpringSecurity、SpringAOP、SpringORM、SpringTest
,这些模块缩短应用程序的开发时间,提高了应用开发的效率例如,在
JavaWeb开发的早期阶段,我们需要编写大量的代码来将记录插入到数据库中。但是通过使用
SpringJDBC模块的JDBCTemplate,我们可以将操作简化为几行代码。
什么是Spring Boot
基本上是框架的扩展,它消除了设置应用程序所需的XML配置,为更快,更高效的开发生态系统铺平了道路。
中的一些特征:
1、创建独立的应用。
2、嵌入式Tomcat、Jetty、Undertow容器(无需部署war文件)。
3、提供的starters简化构建配置
4、尽可能自动配置spring应用。
5、提供生产指标,例如指标、健壮检查和外部化配置6、完全没有代码生成和XML配置要求
从配置分析
Maven依赖
首先,让我们看一下使用Spring创建Web应用程序所需的最小依赖项
org.springframework
spring-web
5.1.0.RELEASE
spring-webmvc
与Spring不同,Spring Boot只需要一个依赖项来启动和运行Web应用程序:
org.springframework.boot
spring-boot-starter-web
2.0.6.RELEASE
在进行构建期间,所有其他依赖项将自动添加到项目中。
另一个很好的例子就是测试库。我们通常使用
SpringTest,JUnit Hamcrest Mockito库。在项目中,我们应该将所有这些库添加为依赖项。但是在SpringBoot中,我们只需要添加spring-boot-starter-test
依赖项来自动包含这些库。
Spring Boot为不同的Spring模块提供了许多依赖项。一些最常用的是:
spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-web
spring-boot-starter-thymeleaf
有关starter的完整列表,请查看Spring文档。
MVC配置
让我们来看一下
需要定义调度程序servlet,映射和其他支持配置。我们可以使用web.xml
文件或Initializer类来完成此操作:
publicclassMyWebAppInitializerimplementsWebApplicationInitializer{
@Override
publicvoid onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context = newAnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pingfangushi");
container.addListener(newContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", newDispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
还需要将
@EnableWebMvc注释添加到
@Configuration类,并定义一个视图解析器来解析从控制器返回的视图:
publicclassClientWebConfigimplementsWebMvcConfigurer{@BeanpublicViewResolver viewResolver() {InternalResourceViewResolver bean= newInternalResourceViewResolver();bean.setViewClass(JstlView.class);bean.setPrefix("/WEB-INF/view/");bean.setSuffix(".jsp");return bean;再来看一旦我们添加了Web启动程序,只需要在application配置文件中配置几个属性来完成如上操作:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
上面的所有Spring配置都是通过一个名为auto-configuration的过程添加
Bootweb starter
来自动包含的。
这意味着将查看应用程序中存在的依赖项,属性和bean
,并根据这些依赖项,对属性和进行配置。当然,如果我们想要添加自己的自定义配置,那么自动配置将会退回。
配置模板引擎
现在我们来看下如何在Spring和Spring Boot中配置Thymeleaf模板引擎。
在中,我们需要为视图解析器添加
thymeleaf-spring5
依赖项和一些配置:
publicclassMvcWebConfigimplementsWebMvcConfigurer{@AutowiredprivateApplicationContext applicationContext;
publicSpringResourceTemplateResolver templateResolver() {SpringResourceTemplateResolver templateResolver = newSpringResourceTemplateResolver();templateResolver.setApplicationContext(applicationContext);templateResolver.setPrefix("/WEB-INF/views/");templateResolver.setSuffix(".html");return templateResolver;
publicSpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = newSpringTemplateEngine();templateEngine.setTemplateResolver(templateResolver());templateEngine.setEnableSpringELCompiler(true);return templateEngine;publicvoid configureViewResolvers(ViewResolverRegistry registry) {ThymeleafViewResolver resolver = newThymeleafViewResolver();resolver.setTemplateEngine(templateEngine());registry.viewResolver(resolver);
SpringBoot1X只需要的依赖项来启用应用程序中的Thymeleaf
支持。但是由于Thymeleaf3.0中的新功能,我们必须将
thymeleaf-layout-dialect添加为SpringBoot2X
Web应用程序中的依赖项。配置好依赖,我们就可以将模板添加到
src/main/resources/templates文件夹中,将自动显示它们。
Spring Security 配置
为简单起见,我们使用框架默认的HTTPBasic身份验证。让我们首先看一下使用
启用Security所需的依赖关系和配置。
首先需要依赖
spring-security-web
spring-security-config模块。接下来, 我们需要添加一个扩展WebSecurityConfigurerAdapter的类,并使用
@EnableWebSecurity注解:
publicclassCustomWebSecurityConfigurerAdapterextendsWebSecurityConfigurerAdapter{
publicvoid configureGlobal(AuthenticationManagerBuilder auth) throwsException{auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("password")).authorities("ROLE_ADMIN");protectedvoid configure(HttpSecurity ) throwsException{.anyRequest().authenticated().and()publicPasswordEncoder passwordEncoder() {returnnewBCryptPasswordEncoder();这里我们使用inMemoryAuthentication来设置身份验证。同样,也需要这些依赖项才能使其工作。但是我们只需要定义的依赖关系,因为这会自动将所有相关的依赖项添加到类路径中。
中的安全配置与上面的相同。
应用程序启动引导配置
中应用程序引导的基本区别在于。使用或SpringServletContainerInitializer作为其引导入口点。仅使用Servlet3功能来引导应用程序,下面让我们详细来了解下
Spring 引导配置
支持传统的引导方式以及最新的Servlet3+方法。配置方法启动的步骤Servlet容器(服务器)读取中定义的DispatcherServlet由容器实例化通过读取WEB-INF/{servletName}-servlet.xml来创建WebApplicationContext。最后,注册在应用程序上下文中定义的
方法的 启动步骤
容器搜索实现ServletContainerInitializer的类并执行找到实现所有类
WebApplicationInitializer``WebApplicationInitializer创建具有XML或上下文类WebApplicationInitializer创建与先前创建的上下文。
SpringBoot 引导配置
Spring Boot应用程序的入口点是使用@SpringBootApplication注释的类
@SpringBootApplication
publicclassApplication{
publicstaticvoid main(String[] args) {
SpringApplication.run(Application.class, args);
默认情况下,使用嵌入式容器来运行应用程序。在这种情况下,publicstaticvoidmain入口点来启动嵌入式服务器。此外,它还负责将FilterServletContextInitializerbean从应用程序上下文绑定到嵌入式容器。的另一个特性是它会自动扫描同一个包中的所有类或Main类的子包中的组件。
提供了将其部署到外部容器的方式。我们只需要扩展SpringBootServletInitializer
即可:
publicclassServletInitializerextendsSpringBootServletInitializer{protectedSpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class);publicvoid onStartup(ServletContext servletContext) throwsServletException{super.onStartup(servletContext);servletContext.addListener(newHttpSessionEventPublisher());
这里外部容器查找在war包下的META-INF文件夹下MANIFEST.MF文件中定义的
Main-class将负责绑定ServletContextInitializer
打包和部署
最后,让我们看看如何打包和部署应用程序。这两个框架都支持Maven
Gradle等通用包管理技术。但是在部署方面,这些框架差异很大。例如,Spring Boot Maven插件在中提供支持。它还允许打包可执行jar war包并
就地运行应用程序。在部署环境中对比的一些优点包括:
1、提供嵌入式容器支持2、使用命令java -jar独立运行jar3、在外部容器中部署时,可以选择排除依赖关系以避免潜在的jar冲突4、部署时灵活指定配置文件的选项5、用于集成测试的随机端口生成
结论
简而言之,我们可以说只是本身的扩展,使开发,测试和部署更加方便。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.