SpringBoot简介及入门
一、SpringBoot简介
1.1原有Spring优缺点分析
1.1.1 Spring的优点分析
Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的Enterprise JavaBean(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的Java对象(Plain Old Java Object,POJO)实现了EJB的功能。
1.1.2Spring的缺点分析
虽然Spring的组件代码是轻量级的,但它的配置却是重量级的。一开始,Spring用XML配置,而且是很多XML配 置。Spring 2.5引入了基于注解的组件扫描,这消除了大量针对应用程序自身组件的显式XML配置。Spring 3.0引入了基于Java的配置,这是一种类型安全的可重构配置方式,可以代替XML。
所有这些配置都代表了开发时的损耗。因为在思考Spring特性配置和解决业务问题之间需要进行思维切换,所以编 写配置挤占了编写应用程序逻辑的时间。和所有框架一样,Spring实用,但与此同时它要求的回报也不少。
除此之外,项目的依赖管理也是一件耗时耗力的事情。在环境搭建时,需要分析要导入哪些库的坐标,而且还需要 分析导入与之有依赖关系的其他库的坐标,一旦选错了依赖的版本,随之而来的不兼容问题就会严重阻碍项目的开 发进度。
1.2SpringBoot的概述
1.2.1SpringBoot解决上述Spring的缺点
SpringBoot对上述Spring的缺点进行的改善和优化,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑 业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短 了项目周期。
1.2.2SpringBoot的特点
为基于Spring的开发提供更快的入门体验
开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式
1.2.3SpringBoot的核心功能
起步依赖
起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。
简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。 自动配置
Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的。
注意:起步依赖和自动配置的原理剖析会在第三章《SpringBoot原理分析》进行详细讲解
二、SpringBoot快速入门
2.1代码实现
2.1.1创建Maven工程
使用idea工具创建一个maven工程,该工程为普通的java工程即可
2.1.2添加SpringBoot的起步依赖
SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent
<!--所有的springboot工程都必须继承spring-boot-starter-parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- aop的pom坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
2.1.3 编写SpringBoot引导类
要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by 54110 on 2019-06-03.
*/
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
2.1.4编写Controller
在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by 54110 on 2019-06-03.
*/
@Controller
public class QuickStartController {
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "Spring Boot 访问成功";
}
}
2.1.5测试
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2019-06-03 16:57:11.817 INFO 12412 --- [ restartedMain] com.MySpringBootApplication : Starting MySpringBootApplication on miaohangbo with PID 12412 (D:\project\springboo_qucik\target\classes started by 54110 in D:\project\springboo_qucik)
2019-06-03 16:57:11.818 INFO 12412 --- [ restartedMain] com.MySpringBootApplication : No active profile set, falling back to default profiles: default
2019-06-03 16:57:11.872 INFO 12412 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@62bb93a5: startup date [Mon Jun 03 16:57:11 CST 2019]; root of context hierarchy
2019-06-03 16:57:13.994 INFO 12412 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-06-03 16:57:14.013 INFO 12412 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-06-03 16:57:14.013 INFO 12412 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2019-06-03 16:57:14.020 INFO 12412 --- [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: [G:\java1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;G:\java1.8\bin;G:\java1.8\jre\bin;G:\python3;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Program Files\Git\cmd;G:\apache-maven-3.5.0\bin;G:\nodejs\;G:\RabbitMqServer\erl9.3\bin;G:\edusoft\ffmpeg-20180227-fa0c9d6-win64-static\bin;G:\OpenSSL-Win64\bin;G:\RabbitMqServer\rabbitmq_server-3.7.8\sbin;G:\Elastic\Ruby25\bin;C:\Users\54110\AppData\Local\Microsoft\WindowsApps;G:\docker\Docker Toolbox;C:\Users\54110\AppData\Roaming\npm;C:\Users\54110\AppData\Local\BypassRuntm;.]
2019-06-03 16:57:14.083 INFO 12412 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-06-03 16:57:14.084 INFO 12412 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2214 ms
2019-06-03 16:57:14.173 INFO 12412 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2019-06-03 16:57:14.176 INFO 12412 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-06-03 16:57:14.176 INFO 12412 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-06-03 16:57:14.176 INFO 12412 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-06-03 16:57:14.176 INFO 12412 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-06-03 16:57:14.256 INFO 12412 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-06-03 16:57:14.384 INFO 12412 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@62bb93a5: startup date [Mon Jun 03 16:57:11 CST 2019]; root of context hierarchy
2019-06-03 16:57:14.438 INFO 12412 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/quick]}" onto public java.lang.String com.qf.controller.QuickStartController.quick()
2019-06-03 16:57:14.440 INFO 12412 --- [ restartedMain] 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)
2019-06-03 16:57:14.441 INFO 12412 --- [ restartedMain] 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)
2019-06-03 16:57:14.458 INFO 12412 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-06-03 16:57:14.458 INFO 12412 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-06-03 16:57:14.572 INFO 12412 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-06-03 16:57:14.592 INFO 12412 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-06-03 16:57:14.680 INFO 12412 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-03 16:57:14.683 INFO 12412 --- [ restartedMain] com.MySpringBootApplication : Started MySpringBootApplication in 3.164 seconds (JVM running for 3.567)
通过日志发现,Tomcat started on port(s): 8080 (http) with context path ‘’ tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空
打开浏览器访问
url 地址为: http://localhost:8080/quick
下一篇: webpack入门
推荐阅读
-
SpringBoot简介及入门
-
springboot中日志简介
-
springboot简介
-
hibernate orm框架介绍篇及入门案例
-
SpringBoot入门:SpringBoot的数据库操作:spring-data-jpa的使用
-
Springboot___快速入门
-
springboot入门测试时,启动服务器后,浏览器报错404无法访问资源
-
Mac系统下搭建OpenGL环境及入门案例
-
【初学maven】单元测试 博客分类: 软件安装及使用 maven单元测试 1.maven-surefire-plugin简介
-
【SpringBoot】springboot整合多数据拆分思路 及 使用分包方式拆分数据源