副问题[/!--empirenews.page--]
1、概述:
对付Spring 和SpringBoot 到底有什么区别,我听到了许多谜底,刚开始迈入进修SpringBoot 的我其时也是一头雾水,跟着履历的蕴蓄、我逐步领略了这两个框架到底有什么区别,我信托对付用了SpringBoot 好久的开拓职员来说,有绝大部门还不是很领略SpringBoot 到底和Spring 有什么区别,看完文章中的较量,或者你有了差异的谜底和观点!
2、什么是Spring呢?
先来聊一聊Spring 作为Java 开拓职员,各人都Spring 可不生疏,简而言之,Spring 框架为开拓Java 应用措施提供了全面的基本架构支持。它包括一些很好的成果,如依靠注入和开箱即用的模块,如:
Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test
这些模块各人应该都用过吧,这些模块收缩应用措施的开拓时刻,进步了应用开拓的服从
譬喻,在Java Web 开拓的早期阶段,我们必要编写大量的代码来将记录插入到数据源中。可是通过行使Spring JDBC 模块的JDBCTemplate ,我们可以将这操纵简化为只需设置几行代码。
3、什么是Spring Boot呢?
Spring Boot 根基上是Spring 框架的扩展,它消除了配置Spring 应用措施所需的XML设置 ,为更快,更高效的开产生态体系铺平了阶梯。
以下是Spring Boot 中的一些特点:
1:建设独立的spring 应用。
2:嵌入Tomcat , Jetty Undertow 并且不必要陈设他们。
3:提供的“starters” poms来简化Maven 设置
4:尽也许自动设置spring 应用。
5:提供出产指标,结实搜查和外部化设置
6:绝对没有代码天生和XML 设置要求
4、让我们慢慢认识这两个框架
4.1、 Maven依靠
起首,让我们看一下行使Spring建设Web应用措施所需的最小依靠项
- <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>
与Spring差异,Spring Boot只必要一个依靠项来启动和运行Web应用措施:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.0.6.RELEASE</version>
- </dependency>
在举办构建时代,全部其他依靠项将自动添加到项目中。
另一个很好的例子就是测试库。我们凡是行使Spring Test ,JUnit ,Hamcrest 和Mockito 库。在Spring 项目中,我们应该将全部这些库添加为依靠项。可是在Spring Boot中 ,我们只必要添加spring-boot-starter-test 依靠项来自动包括这些库。
Spring Boot为差异的Spring模块提供了很多依靠项。一些最常用的是:
spring-boot-starter-data-jpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
有关starter 的完备列表,请查察Spring文档。
4.2、MVC设置
让我们来看一下Spring 和Spring Boot 建设JSP Web 应用措施所需的设置。
Spring 必要界说调治措施servlet ,映射和其他支持设置。我们可以行使 web.xml 文件或Initializer 类来完成此操纵:
- public class MyWebAppInitializer implements WebApplicationInitializer {
-
- @Override
- public void onStartup(ServletContext container) {
- AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
- context.setConfigLocation("com.pingfangushi");
- container.addListener(new ContextLoaderListener(context));
- ServletRegistration.Dynamic dispatcher = container
- .addServlet("dispatcher", new DispatcherServlet(context));
- dispatcher.setLoadOnStartup(1);
- dispatcher.addMapping("/");
- }
- }
(编辑:河北网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|