本文共 1728 字,大约阅读时间需要 5 分钟。
使用IDEA工具创建一个maven工程,该工程为普通工厂即可
SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent
org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE
SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖
org.springframework.boot spring-boot-starter-web
要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问
package com.musk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;// 声明该类是一个 SpringBoot引导类@SpringBootApplicationpublic class MySpringBootApplication { // main 是java程序的入口 public static void main(String[] args) { // run 方法: 表示要运行 springboot 的引导类,run参数就是SpringBoot引导类的字节码对象 SpringApplication.run(MySpringBootApplication.class); }}
在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController
package com.musk.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @Author Cmushi * @create 2020/4/13 16:29 */@Controllerpublic class QuickController { @RequestMapping("/quick") @ResponseBody public String quick(){ return " 学习 springboot"; }}
执行SpringBoot起步类的主方法,控制台打印日志如下:
通过日志发现,Tomcat started on port(s): 8080 (http) with context path ‘’ tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空 打开浏览器访问url地址为:http://localhost:8080/quick 快速入门完毕!此外,经过源码查找,如图:
其中,prefix = “server” 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类的字段中。我们可以配置服务器的端口,如图:
我们不仅配置了服务器端口,还增加了当前Web应用的名称转载地址:http://aswpi.baihongyu.com/