SpringBoot 入门知识点详解
Spring Boot 入门项目
- springboot 介绍
- springboot 引言
- springboot 特点
- springboot 约定大于配置
- springboot 入门项目
- 1、引入项目依赖
- 2、引入配置文件
- yml 基础语法
- 3、 建包并创建控制器 HelloController
- 4、编写入口类 Application
- 5、启动项目并访问
- 相关注解
- @EnableAutoConfiguration
- @ComponentScan
- @RestController
- @RequestMapping
SpringBoot 知识点目录: SpringBoot 核心知识点整理!
springboot 介绍
springboot 引言
Spring Boot 是由 Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了 特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
springboot(微框架) = springmvc(控制器) + spring core(项目管理)
框架的演变过程:
- SSH: Spring — Struts — Hibernate
- SSM: Spring — Struts2 | Struts1 — Mybatis
- SSM: Spring — SpringMVC — Mybatis
springboot 特点
- 创建独立的 Spring 应用程序
- 嵌入的 Tomcat,无需部署 WAR 文件
- 简化 Maven 配置
- 自动配置 Spring,没有 XML 配置!
springboot 约定大于配置
项目目录结构:
springboot 项目中必须在 src/main/resources
中 放入 application.yml
(application.properties
)核心配置文件,名字必须为:application
;
springboot 项目中必须在 src/main/java
中所有子包之外构建全局入口类型 xxApplication
,入口类一个 springboot 项目只能有一个。
springboot 入门项目
项目整体结构:
1、引入项目依赖
<!--继承springboot的父项目-->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version>
</parent><dependencies><!--引入springboot的web支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
2、引入配置文件
配置文件位于:src/main/resources/application.yml
;
可以通过修改端口号解决 tomcat 端口占用问题:
server:port: 8989#用来指定内嵌服务器端口号context-path: /springboot #用来指定项目的访问路径
yml 基础语法
person:lastName: helloage: 18boss: falsebirth: 2017/12/12maps: {k1: v1,k2: 12} lists: # 数组- lisi- zhaoliudog:name: 小狗age: 12
3、 建包并创建控制器 HelloController
@RestController
@RequestMapping("/hello")
public class HelloController {@GetMapping("/hello")public String hello() {System.out.println("hello springboot!!!");return "hello springboot";}
}
4、编写入口类 Application
// @EnableAutoConfiguration // 作用: 开启自动配置 初始化spring环境 springmvc环境
// @ComponentScan // 作用: 用来扫描相关注解 扫描范围 当前入口类所在的包及子包(com.yusal及其子包)
// 下面的注解的功能是上面两个注解功能之和
@SpringBootApplication
public class Application {public static void main(String[] args) {// springApplication: spring应用类 作用: 用来启动springboot应用// 参数1: 传入入口类 类对象 参数2: main函数的参数SpringApplication.run(Application.class, args);}
}
5、启动项目并访问
运行 Application 中的 main 来启动整个项目:如下则启动成功。
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.2.5.RELEASE)2020-06-19 21:56:45.166 INFO 8888 --- [ main] com.yusael.Application : Starting Application on DESKTOP-PNEMUCC with PID 8888 (D:\CodePro\IdeaPro\SpringBoot\hello_springboot\target\classes started by yusael in D:\CodePro\IdeaPro\SpringBoot)
2020-06-19 21:56:45.171 INFO 8888 --- [ main] com.yusael.Application : No active profile set, falling back to default profiles: default
2020-06-19 21:56:46.716 INFO 8888 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8989 (http)
2020-06-19 21:56:46.730 INFO 8888 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-19 21:56:46.731 INFO 8888 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-06-19 21:56:46.855 INFO 8888 --- [ main] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring embedded WebApplicationContext
2020-06-19 21:56:46.856 INFO 8888 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1613 ms
2020-06-19 21:56:47.244 INFO 8888 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-19 21:56:47.473 INFO 8888 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8989 (http) with context path '/hello_springboot'
2020-06-19 21:56:47.487 INFO 8888 --- [ main] com.yusael.Application : Started Application in 3.253 seconds (JVM running for 4.806)
2020-06-19 21:57:28.156 INFO 8888 --- [nio-8989-exec-3] o.a.c.c.C.[.[.[/hello_springboot] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-19 21:57:28.157 INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-19 21:57:28.165 INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
注意:springboot 的项目默认没有项目名,项目名要在 application.yml
中配置;
如果不在配置文件中配置,路径应该是:
http://localhost:8080/hello/hello
由于我们在配置文件中进行了配置:
server:port: 8989#用来指定内嵌服务器端口号context-path: /springboot #用来指定项目的访问路径
所以访问路径变成了:
http://localhost:8989/springboot/hello/hello
相关注解
@EnableAutoConfiguration
- 作用:开启自动配置 ,根据 pom.xml 文件中依赖自动判断并构建环境
如在第一个环境之中引入了 spring-boot-starter-web, 会自动根据引入的这个依赖构建相关环境(springmvc环境、web容器环境) - 修饰范围:只能用在类上
@ComponentScan
- 作用:用来开启注解扫描
- 修饰范围:只能用在类上
- 扫描注解范围:默认当前包以及当前包下的子包
@SpringBootApplication
:@EnableAutoConfiguration
+ @ComponentScan
@RestController
- 作用:用来实例化当前对象为一个控制器对象,并将类上的所有方法的返回值转为 json,响应给浏览器。
- 修饰范围: 用在类上
- 功能相当于:
@Controller
+@ResponseBody
@Controller
:实例化当前类为一个控制器@ResponseBody
:用来将当前方法返回值转为 json, 相应给浏览器
@RequestMapping
- 作用:用来加入访问路径
- 修饰范围:类(加入命名空间)、方法上(指定具体路径)
- 同类注解:
@GetMapping
:作用: 限定请求方式只能是GET,并指定路径;修饰范围:方法上
@PostMapping
@DeleteMapping
@PutMapping
main
方法的作用:指定通过标准 java 入口方式委托给 SpringApplication
,并告知当前 springboot
主应用类是谁,从而启动 springboot 中 tomcat 容器;
args
作用:可以在启动时使用外部参数
starter
:启动器,springboot-starter-xxx Starters 是一组方便的依赖关系描述符。
SpringBoot 入门知识点详解相关推荐
- SpringBoot所有知识点详解,根据狂神说java老师的整理
1.SpringBoot:Hello,World! SpringBoot简介 1.1.回顾什么是Spring Spring是一个开源框架,2003 年兴起的一个轻量级的Java 开发框架,作者:Rod ...
- springboot入门demo详解(解决跨域)
前言: 微服务自问世以来就一直很火.天天耳听Springboot,Springcloud,docker,这些名词,以前没学Springboot的时候刚学ssm的时候,,说实话头都炸了.单独一个个学习S ...
- springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解
在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...
- java框架魔乐_16 魔乐科技 SpringBoot框架开发详解
资源内容: 16 魔乐科技 SpringBoot框架开发详解|____springboot开发代码.rar|____第一章:SpringBoot入门 |____2. SpringBo ...
- Springboot传参详解
作者简介 作者名:编程界明世隐 简介:CSDN博客专家,从事软件开发多年,精通Java.JavaScript,博主也是从零开始一步步把学习成长.深知学习和积累的重要性,喜欢跟广大ADC一起打野升级,欢 ...
- python 快速排序_小白入门知识详解:Python实现快速排序的方法(含实例代码)...
前言: 今天为大家带来的内容是:小白入门知识详解:Python实现快速排序的方法(含实例代码)希望通过本文的内容能够对各位有所帮助,喜欢的话记得点赞转发收藏不迷路哦!!! 提示: 这篇文章主要介绍了P ...
- SpringBoot异常处理ErrorController详解
文章目录 一.背景 二.SpringBoot的默认异常处理BasicErrorController 三.自定义错误异常 写在前面: 我是「境里婆娑」.我还是从前那个少年,没有一丝丝改变,时间只不过是考 ...
- SpringBoot的配置详解application
SpringBoot的配置文件application有两种文件格式,两种配置的内容是一致的,只是格式不一致. 1.application.properties 2.application.yml或者a ...
- Zookeeper知识点详解
Zookeeper知识点详解 目录 ZooKeeper 集群原理 ZooKeeper 分布式锁 ZooKeeper 分布式事务 ZooKeeper 选举原理 Paxos 协议 ZAB 协议 ZooKe ...
最新文章
- display会影响canvas吗_多动症会影响智商吗?
- no awt in java.libary.path
- 9b9t服务器显示连接超时,Fabric-sdk-go实例化链码连接7053端口等待超时
- matlab中的神经网络训练,MATLAB中的神经网络训练
- 哈佛成功金句 -25则
- 北京 | Aibee 爱笔智能 招聘 计算机视觉算法实习生
- [BEC][hujiang] Lesson02 Unit1:Working life ---Reading
- 【经验分享】如何搭建本地MQTT服务器(Windows ),并进行上下行调测
- 函数 单片机glint_PIC单片机CCS之C语言(#BIT的用法)
- linux系统怎么ping命令,Linux系统下的ping命令的使用
- oracle服务端导出/导入方式expdp/impdp
- 中国地质大学网络计算机考试试题,中国地质大学《计算机》考试题答案
- 2017年11月软考全国各省市报名时间及报名网址(动态更新)
- 1数字电路设计流程与SOC芯片架构图
- 太原理工大计算机专业几本,太原理工大学是几本 学生评价怎么样好不好(10条)...
- [玩法/技巧] Transmission 3.0 降级到 2.94 恢复数据的方法
- CO BW成本报表建立小结
- 常见机器学习算法适合使用的业务场景汇总(1)
- 开工干活累了,晚上不得找个陪玩打打游戏?我教你们用python找个人美声甜的
- 红米4a android 9 速度,雷军感叹科技进步速度太快!Redmi 9入门机性能已经相当于骁龙835...