一、SpringBoot的优点和Spring的缺点
1、SpringBoot的优点
Spring是Java企业版(Java Enterprise Editon,也称J2EE)的轻量级代替品。无需开发重量级的Enterprise JavaBean (EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的EJB对象实现了EJB的功能
2、Spring的缺点
虽然Spring的组件代码是轻量级的,但是它的配置却是重量级的,一开始Spring用xml配置,而且是很多xml配置,spring2.5引入了基于注解的组件扫描,这消除了大量针对应用程序自身组件的显示xml配置,spring3.0引入了基于Java的配置,这是一种类型安全的可重构配置方式,可以代替xml
所有这些配置都代表了开发时的损耗,因为在思考Spring特性配置和解决业务问题需要进行思维切换,所以编写配置挤占了编写应用程序逻辑的时间,和所有框架异应,Spring实用,但与此同时他要求的回报也不少,除此之外,项目的依赖管理也是一件极为耗力的事情在环境搭建时,需要分析要导入那些库的坐标而且还需要分析导入与之有依赖关系的其他库的坐标,一旦选错了依赖版本,随之而来的不兼容问题就会严重阻碍项目的开发进度。
二、SpringBoot简述
SpringBoot对上述的Spring缺点进行的改善和优化,基于约定优于配置的思想(因为一些配置都是一样的),可以让开发人员不必在配置与逻辑业务期间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期
SpringBoot特点
- 为基于Spring的开发提供更快的入门体验
- 开箱即用 ,没有代码生成,也无需xml配置,也同时可以修改默认值来满足特定的需求
- 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标、健康检测、外部配置等
- SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式
三、SpringBoot的核心功能
1、起步依赖
起步依赖本质上是一个maven项目对象模型,定义了对其他库的传递依赖。这些东西加在一起即支持某项功能
2、自动配置
SpringBoot的自动配置是一个运行时(更准确的说,是应用程序启动时)的过程,考虑了众多因素才决定Spring配置应用用哪一个,不该用那个,该过程是Spring自动配置完成的
四、入门程序
1、创建maven工程
2、添加SpringBoot的起步依赖
1)项目继承Spring Boot的起步依赖,在pom.xml文件中添加
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.1.RELEASE</version> 5 </parent>
2) SpringBoot要即成SpringMVC进行Controller 开发,所以项目要导入web的启动依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 </dependencies>
3、编写SpringBoot引导类
1) 要通过springBoot提供的引导类起步,springboot才可以进行访问
2)在启动类上添加@SpringBootApplication注解
4、运行启动类
D:Program FilesJavajdk1.8.0_91injava.exe"
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )\___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2021-02-01 23:26:05.685 INFO 10484 --- [ main] com.ssc.MyApplication : Starting MyApplication on LAPTOP-M8T08JIL with PID 10484 (D:workspaceideasscSpringBoot_demo1 argetclasses started by SSC in D:workspaceideasscSpringBoot_demo1)
2021-02-01 23:26:05.687 INFO 10484 --- [ main] com.ssc.MyApplication : No active profile set, falling back to default profiles: default
2021-02-01 23:26:05.711 INFO 10484 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c153a1: startup date [Mon Feb 01 23:26:05 GMT+08:00 2021]; root of context hierarchy
2021-02-01 23:26:06.225 INFO 10484 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-02-01 23:26:06.237 INFO 10484 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-02-01 23:26:06.237 INFO 10484 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2021-02-01 23:26:06.240 INFO 10484 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:Program FilesJavajdk1.8.0_91in;C:windowsSunJavain;C:windowssystem32;C:windows;C:ProgramDataOracleJavajavapath;C:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:windowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program FilesNVIDIA CorporationNVIDIA NGX;C:Program FilesNVIDIA CorporationNVIDIA NvDLISR;D:Program Filespython34;D:Program Filesapache-maven-3.6.3in;D:Program Files
odejs;D:Program FilesJavajdk1.8.0_91in;D:Program FilesJavajdk1.8.0_91jrein;D:Program FilesTortoiseSVNin;D:Program FilesGitcmd;D:Program Filesgradle-6.6.1in;C:UsersAdministratorAppDataRoaming
pm;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;;.]
2021-02-01 23:26:06.286 INFO 10484 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-02-01 23:26:06.286 INFO 10484 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 577 ms
2021-02-01 23:26:06.345 INFO 10484 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2021-02-01 23:26:06.347 INFO 10484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2021-02-01 23:26:06.348 INFO 10484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2021-02-01 23:26:06.348 INFO 10484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2021-02-01 23:26:06.348 INFO 10484 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2021-02-01 23:26:06.403 INFO 10484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-02-01 23:26:06.508 INFO 10484 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c153a1: startup date [Mon Feb 01 23:26:05 GMT+08:00 2021]; root of context hierarchy
2021-02-01 23:26:06.541 INFO 10484 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2021-02-01 23:26:06.541 INFO 10484 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2021-02-01 23:26:06.554 INFO 10484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-02-01 23:26:06.554 INFO 10484 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2021-02-01 23:26:06.628 INFO 10484 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2021-02-01 23:26:06.648 INFO 10484 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-02-01 23:26:06.650 INFO 10484 --- [ main] com.ssc.MyApplication : Started MyApplication in 1.138 seconds (JVM running for 1.669)
5、启动测试
6、添加Conteoller