面试官:spring boot和spring究竟有啥区别?

面试官:spring boot和spring究竟有啥区别?

前言

今天本篇文章主要聚焦说说,spring boot和spring究竟有啥区别,重点对比MVC模块以及Security模块在两大框架使用时的区别。

1 、啥是spring?

简而言之,Spring框架为开发Java应用程序提供了全面的基础架构支持。

它包含一些很好的功能,如依赖注入和开箱即用的模块,如:

  • Spring JDBC
  • Spring MVC
  • Spring Security
  • Spring AOP
  • Spring ORM
  • Spring Test

这些模块可以大大缩短应用程序的开发时间。

例如,在Java Web开发的早期阶段,我们需要编写大量的重复代码来将记录插入到数据源中。但是通过使用Spring JDBC模块的JDBCTemplate,我们可以将它简化为只需几个简单配置或者几行代码。

2,啥是spring boot?

Spring Boot基本上是Spring框架的扩展,它消除了设置Spring应用程序所需的复杂例行配置。

它的目标和Spring的目标是一致的,为更快,更高效的开发生态系统铺平了道路。

以下是Spring Boot中的一些功能:

  • 通过starter这一个依赖,以简化构建和复杂的应用程序配置
  • 可以直接main函数启动,嵌入式web服务器,避免了应用程序部署的复杂性
  • Metrics度量,Helth check健康检查和外部化配置
  • 自动化配置Spring功能 - 尽可能的

下面进一步详述一下两者的区别:

3 、Maven依赖

首先,让我们看一下使用Spring创建Web应用程序所需的最小依赖项:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>5.1.0.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>5.1.0.RELEASE</version>
</dependency>
</pre>

与Spring不同,Spring Boot只需要一个依赖项来启动和运行Web应用程序:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <version>2.0.5.RELEASE</version>
</dependency>
</pre>

在构建期间,所有其他依赖项将自动添加到最终归档中。

另一个很好的例子是测试库。我们通常使用Spring Test,JUnit,Hamcrest和Mockito库集。在Spring项目中,我们应该将所有这些库添加为依赖项。

但是在Spring Boot中,我们只需要用于测试的启动器依赖项来自动包含这些库。

Spring Boot为不同的Spring模块提供了许多入门依赖项。一些最常用的是:

  • spring-boot-starter-data-jpa
  • spring-boot-starter-security
  • spring-boot-starter-test
  • spring-boot-starter-web
  • spring-boot-starter-thymeleaf

4 、MVC配置

下面来探讨一下使用Spring和Spring Boot创建JSP Web应用程序所需的配置。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class MyWebAppInitializer implements WebApplicationInitializer {
 @Override
 public void onStartup(ServletContext container) {
 AnnotationConfigWebApplicationContext context
 = new AnnotationConfigWebApplicationContext();
 context.setConfigLocation("com.test.package");
 container.addListener(new ContextLoaderListener(context));
 ServletRegistration.Dynamic dispatcher = container
 .addServlet("dispatcher", new DispatcherServlet(context));
 dispatcher.setLoadOnStartup(1);
 dispatcher.addMapping("/");
 }
}
</pre>

我们还需要将@EnableWebMvc注解添加到@Configuration注解类,并定义一个视图解析器来解析从控制器返回的视图:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@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;
 }
}
</pre>

与所有这些相比,一旦我们添加了Spring boot web starter,Spring Boot只需要一些属性来使上面的事情正常工作:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
</pre>

上面的所有Spring配置都是通过一个名为auto-configuration的进程添加Boot web starter来自动包含的。

这意味着Spring Boot将自动扫描应用程序中存在的依赖项,属性和bean,并根据这些内容启用相应的配置。

5 、配置模板引擎

再来看看如何在Spring和Spring Boot中配置Thymeleaf模板引擎,两者有啥区别?

在Spring中,我们需要为视图解析器添加 thymeleaf-spring5依赖项和一些配置:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@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);
 }
}
</pre>

Spring Boot 只需要spring-boot-starter-thymeleaf的依赖项 来启用Web应用程序中的Thymeleaf支持。

一旦依赖关系添加成功后,我们就可以将模板添加到src / main / resources / templates文件夹中,Spring Boot将自动显示它们。

6 、安全配置

为简单起见,我们将看到如何使用这些框架启用默认的HTTP Basic身份验证。

让我们首先看一下使用Spring启用Security所需的依赖关系和配置。

Spring需要标准的 spring-security-web和spring-security-config 依赖项来在应用程序中设置Security。

接下来, 我们需要添加一个扩展WebSecurityConfigurerAdapter的类,并使用@EnableWebSecurity注解:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
 auth.inMemoryAuthentication()
 .withUser("user1")
 .password(passwordEncoder()
 .encode("user1Pass"))
 .authorities("ROLE_USER");
 }
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 http.authorizeRequests()
 .anyRequest().authenticated()
 .and()
 .httpBasic();
 }
 @Bean
 public PasswordEncoder passwordEncoder() {
 return new BCryptPasswordEncoder();
 }
}
</pre>

同样,Spring Boot也需要这些依赖项才能使其工作。但是我们只需要定义spring-boot-starter-security的依赖关系,它会自动将所有相关的依赖项添加到类路径中。

7、 应用引导Application Bootstrap

Spring和Spring Boot中应用程序引导的基本区别在于servlet。

Spring使用web.xml 或SpringServletContainerInitializer 作为其引导入口点。

spring boot仅仅使用Servlet 3来引导程序

来说说spring引导

web.xml引导方法

  1. Servlet容器(服务器)读取web.xml
  2. web.xml中定义的DispatcherServlet由容器实例化
  3. DispatcherServlet通过读取WEB-INF / {servletName} -servlet.xml来创建WebApplicationContext
  4. 最后,DispatcherServlet注册在应用程序上下文中定义的bean

servlet 3+引导方法

  1. 容器搜索实现ServletContainerInitializer的 类并执行
  2. SpringServletContainerInitializer找到实现类WebApplicationInitializer的子类
  3. WebApplicationInitializer创建会话使用XML或上下文@Configuration类
  4. WebApplicationInitializer创建DispatcherServlet,使用先前创建的上下文。

再来说说spring boot引导

Spring Boot应用程序的入口点是使用@SpringBootApplication注释的类:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootApplication
public class Application {
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}
</pre>

默认情况下,Spring Boot使用嵌入式容器来运行应用程序。在这种情况下,Spring Boot使用public static void main入口点来启动嵌入式Web服务器。

此外,它还负责将Servlet,Filter和ServletContextInitializer bean从应用程序上下文绑定到嵌入式servlet容器。

Spring Boot的另一个特性是它会自动扫描同一个包中的所有类或Main类的子包中的组件。

Spring Boot提供了将其部署为外部容器中的Web存档的选项。在这种情况下,我们必须扩展SpringBootServletInitializer:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootApplication
public class Application extends SpringBootServletInitializer {
 // ...
}
</pre>

外部servlet容器查找在Web归档文件的META-INF文件中定义的Main-class,SpringBootServletInitializer将负责绑定Servlet,Filter和ServletContextInitializer。

8 、打包和部署

最后,让我们看看如何打包和部署应用程序。这两个框架都支持Maven和Gradle等常见的包管理技术。但是在部署方面,这些框架差异很大。

例如,Spring Boot Maven插件在Maven中提供Spring Boot支持。它还允许打包可执行jar或war档案并“就地”运行应用程序。

与spring相比,在部署环境中Spring Boot的一些优点包括

  • 提供嵌入式容器支持
  • 使用命令java -jar独立运行jar
  • 在外部容器中部署时,可以选择排除依赖关系以避免潜在的jar冲突
  • 部署时灵活指定配置文件的选项
  • 用于集成测试的随机端口生成

9 、小结

本篇,讲述了Spring boot和Spring之间的区别。用一句话概况就是:Spring Boot只是Spring本身的扩展,使开发,测试和部署更加方便。

END

如发现文章有错误、对内容有疑问,给我留言哦~

彩蛋小福利

点击免费获取Java学习笔记,面试,文档以及视频

部分资料如下:

image
image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,710评论 4 376
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,839评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,295评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,776评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,198评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,074评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,200评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,986评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,733评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,877评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,348评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,675评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,393评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,209评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,212评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,003评论 2 280

推荐阅读更多精彩内容

  • Spring Boot Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spr...
    AlbenXie阅读 1,824评论 0 18
  • “ 该系列的文章主要参考Spring Boot的官方文档来进行翻译,逐步将官方文档翻译过来,方便大家共同学习和讨论...
    tearofthemyth阅读 1,903评论 0 1
  • 对于Spring和SpringBoot到底有什么区别,我听到了很多答案,刚开始迈入学习SpringBoot的我当时...
    心空如大海阅读 700评论 0 1
  • 刚毕业参加工作那两年,公司有两个女同事。一个叫小青,性格豪爽,童颜巨乳,什么话都敢说,很黄很暴力的大姐范儿;一个叫...
    澄明_之境阅读 745评论 8 19
  • 《春寒心思》 春风吹花花不语, 春雨送寒寒又舞。 春霜挂枝枝成玉, 春雾锁心心如雾。
    我本善良海尔店阅读 165评论 0 3