1. 代码实现
1.1 创建Maven工程


1.2 添加SpringBoot的起步依赖
SpringBoot要求,项目要继承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>
<!--web功能的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1.3 编写SpringBoot引导类
要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问
package com.neuedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//声明该类是springboot的一个引导类
@SpringBootApplication
public class MySpringBootApplication {
//main是java程序的入口
public static void main(String[] args) {
//run方法 表示运行SpringBoot的引导类 run参数就是SpringBoot引导类的字节码对象
SpringApplication.run(MySpringBootApplication.class);
}
}



虽然是错误页面,但是已经可以看出可以访问内置的tomcat,但是注意,该tomcat与我们之前配置过的tomcat不同,它没有默认的页面

1.4 编写Controller
在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController
package com.neuedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickStartController {
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 访问成功!";
}
}

1.5 测试
重启tomcat,执行SpringBoot起步类的主方法,访问后显示效果:

2. 快速入门解析
2.1 SpringBoot代码解析
- @SpringBootApplication:标注SpringBoot的启动类,该注解具备多种功能(后面详细剖析)
- SpringApplication.run(MySpringBootApplication.class) 代表运行SpringBoot的启动类,参数为SpringBoot
启动类的字节码对象
2.2 SpringBoot工程热部署
开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,我们可以在修改代码后不重启就能生效,在 pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署。
注意:Intellij IEDA默认情况下不会自动编译,所以需要实现热部署,还需要对IDEA进行自动编译的设置。

Ctrl+Shift+Alt+/,选择Registry


勾选后close关闭
2. 3 IDEA快速创建SpringBoot项目



选择springboot需要的启动依赖,这里我们先勾选一个web



如果导入不了springboot
可以修改setting
<mirror>
<id>nexus</id>
<name>internal nexus repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
或者
<mirror>
<id>aliyun-maven</id>
<mirrorOf>*</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

package com.neuedu.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@RestController等同于@Controller + @ResponseBody
@RestController
public class QuickController {
@RequestMapping("/quick2")
public String quick(){
return "springBoot !!!";
}
}

网友评论