SpringBoot
...大约 3 分钟
SpringBoot
简介
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
官网
创建初始工程

SpringBoot优点
自动配置
起步依赖
辅助功能(内置tomcat 服务器)
配置文件三种格式
平常主要使用yml格式的
server.port=80
server:
port: 81
server:
port: 82
yml和yaml文件每个 :后面要有一个空格再填写内容
三个文件都有且都在resources目录下则优先级 properties > yml > yaml

此种目录级别下config目录下的优先级要高
yml
YML是一种数据序列化格式
优点
- 容易阅读
- 容易与脚本语言交互
- 是以数据为核心,重数据轻格式
语法规则
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- 注释用 #
数组形式
likes:
- music
- game
- run
读取配置文件
方式一
package com.tensoflow.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class userController
{
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${likes[0]}")
private String like;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id)
{
System.out.println(lesson);
System.out.println(port);
System.out.println(like);
return "Hello World!";
}
}
server:
port: 8080
lesson: SpringBoot
likes:
- music
- game
- run
方式二
package com.tensoflow.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class userController
{
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id)
{
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("likes[0]"));
return "Hello World!";
}
}
server:
port: 8080
lesson: SpringBoot
likes:
- music
- game
- run
第三种方式
package com.tensoflow.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise
{
private String name;
private Integer age;
private String tel;
private String[] subject;
@Override
public String toString()
{
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject=" + Arrays.toString(subject) +
'}';
}
public void setName(String name) {this.name = name;}
public void setAge(Integer age) {this.age = age;}
public void setTel(String tel) {this.tel = tel;}
public void setSubject(String[] subject) {this.subject = subject;}
}
server:
port: 8080
enterprise:
name: itcast
age: 16
tel: 13308614582
subject:
- Java
- Spring
- SpringMVC
- SpringBoot
package com.tensoflow.controller;
import com.tensoflow.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
public class userController
{
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id)
{
System.out.println(enterprise);
return "Hello World!";
}
}
多环境开发配置
yml或者yaml格式
不同的环境用 --- 分开
# 设置启用环境
spring:
profiles:
active: dev
---
# 开发环境
spring:
config:
activate:
on-profile: dev
server:
port: 80
---
# 生产环境
spring:
config:
activate:
on-profile: pro
server:
port: 81
---
# 测试环境
spring:
config:
activate:
on-profile: test
server:
port: 82
properties格式
在application.properties文件中设置如下
spring.profiles.active=dev
必须创建一个名为application-dev.properties文件并在里面写配置
如果是
spring.profiles.active=pro
则必须创建一个名为application-pro.properties文件并在里面写配置
注意
执行Maven中的package命令之前先执行clean命令。如果配置文件里面有中文注释进行Maven打包的时候有可能会失败需要更改Settings中的File Encodings全配置为UTF-8
前端页面存放位置

前端页面放在static目录下
关闭banner图案
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/Study?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
# 关闭SpringBoot的banner
main:
banner-mode: off
Powered by Waline v2.15.8