Spring boot 和Spring的主要区别

1.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-test
  • spring-boot-starter-web
  • spring-boot-starter-thymeleaf

2.配置模板引擎
在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将自动显示它们。

3. 应用引导Application Bootstrap

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

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

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

来说说spring引导

web.xml引导方法

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

容器搜索实现ServletContainerInitializer的 类并执行
SpringServletContainerInitializer找到实现类WebApplicationInitializer的子类
WebApplicationInitializer创建会话使用XML或上下文@Configuration类
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 extends SpringBootServletInitializer {
 // ...
}
</pre>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值