Spring Boot是一个简化Spring开发的框架。用来监护spring应用开发,约定大于配置,去繁就简,just run 就能创建一个独立的,产品级的应用。
用来代替springMvc
Spring Boot 自带Tomcat
简单使用
1.IDEA创建springboot项目
- 选择Spring Initializr
- 设置启动项的包名和类名
- 选择Web–>Spring Web
- 点击finish完成
- 等待文件加载完成后
- SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序
- SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文
- application.properties:一个空的 properties 文件,可以根据需要添加配置属性
- pom.xml: Maven 构建说明文件
- 在java文件夹下新建com.action文件夹,另写一个测试类HelloAction
package com.action;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloAction {
@RequestMapping("/hello")
public String hello(){
return "HelloWorld";
}
}
- @RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版
注意如果不在同一个包内要为启动函数添加扫描包名
如果有两个不同的包则要添加两个扫描且包名不同中的类名也不能相同
@ComponentScan({"包名1","包名2"})
为启动函数添加扫描
package cn.wmyskxz.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.action"}) //添加扫描
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
若有多个文件夹则添加多个扫描
@ComponentScan({“com.action”,“com.action2”})
- 运行启动函数
可以看到SpringBoot自带Tomcat端口号,默认为8080
- 打开浏览器输入http://127.0.0.1:8080/hello显示出HelloWorld即代表启动成功