一、Spring Boot概述
1. 什么是Spring Boot?
2. Spring Boot优点
- 敏捷开发
- 减少xml配置文件,用properties文件
- 内置web容器
二、入门案例(Idea)
1. 创建Spring Initializr
2. 添加spring boot父类依赖和spring boot web依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--引入springboot父类依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lt</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--spring boot-web依赖 spring mvc+spring+mybatis-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 创建测试类,并创建主函数
package com.lt.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName HelloWorld
* @Description spring boot 例子
* @Author lt
* @Date 2019-04-05
**/
/**
* 1.@RestController:表示该类中所有方法的返回值都是json格式
* 2.@EnableAutoConfiguration
*/
@RestController
public class HelloWorld {
@RequestMapping("/getMsg")
public String getMsg() {
return "你好,Spring Boot";
}
public static void main(String[] args){
SpringApplication.run(HelloWorld.class, args);
}
}
4. 启动项目,访问
三、Spring Boot常用注解
- @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解;
- @ComponentScan:让spring boot扫描到Configuration类并把它加入到程序上下文
- @Configuration:等同于spring的XML配置文件;使用Java代码可以检查类型安全
- @EnableAutoConfiguration:自动配置
- @RestController:@Controller和@ResponseBody的合集,表示该类中所有方法的返回值都是json格式
四、spring boot两种启动方式
1. 单独启动:直接运行main方法
package com.lt.springboot.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName Login
* @Description TODO
* @Author lt
* @Date 2019-04-05
**/
@RestController
public class Login {
@RequestMapping("/login")
public Map<String, Object> login(){
Map<String, Object> map = new HashMap<>();
map.put("username", "刘涛");
map.put("password", "123456");
return map;
}
public static void main(String[] args){
SpringApplication.run(Login.class, args);
}
}
2. 设置包扫描路径
- 利用注解@ComponentScan(“com.lt.springboot”)
package com.lt.springboot.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* @ClassName App
* @Description TODO
* @Author lt
* @Date 2019-04-05
**/
@ComponentScan("com.lt.springboot")
@EnableAutoConfiguration
public class App {
public static void main(String[] args){
SpringApplication.run(App.class, args);
}
}
五、静态资源访问
1. spring boot默认配置
- Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
- /static
- /public
- /resources
- /META-INF/resources
- 访问