| 副标题[/!--empirenews.page--]  1、概述: 对于2、什么是Spring呢?Spring和SpringBoot到底有什么区别,我听到了很多答案,刚开始迈入学习SpringBoot的我当时也是一头雾水,随着经验的积累、我慢慢理解了这两个框架到底有什么区别,我相信对于用了SpringBoot很久的开发人员来说,有绝大部分还不是很理解SpringBoot到底和Spring有什么区别,看完文章中的比较,或许你有了不同的答案和看法! 先来聊一聊3、什么是Spring Boot呢?Spring作为Java开发人员,大家都Spring可不陌生,简而言之,Spring框架为开发Java应用程序提供了全面的基础架构支持。它包含一些很好的功能,如依赖注入和开箱即用的模块,如:
 Spring JDBC 、Spring MVC 、Spring Security、 Spring AOP 、Spring ORM 、Spring Test这些模块大家应该都用过吧,这些模块缩短应用程序的开发时间,提高了应用开发的效率
 例如,在
 Java Web开发的早期阶段,我们需要编写大量的代码来将记录插入到数据源中。但是通过使用Spring JDBC模块的JDBCTemplate,我们可以将这操作简化为只需配置几行代码。 Spring Boot基本上是Spring框架的扩展,它消除了设置Spring应用程序所需的XML配置,为更快,更高效的开发生态系统铺平了道路。
 以下是Spring Boot中的一些特点:1:创建独立的4、让我们逐步熟悉这两个框架spring应用。2:嵌入
 Tomcat,JettyUndertow而且不需要部署他们。3:提供的“starters” poms来简化
 Maven配置4:尽可能自动配置
 spring应用。5:提供生产指标,健壮检查和外部化配置
 6:绝对没有代码生成和
 XML配置要求 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("/");     } } 
 (编辑:源码网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |