索引
包结构
使用Maven构建SpringBoot项目:
按照包结构创建包和文件:
引入jar包
<?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>
<groupId>com.imooc</groupId>
<artifactId>myspringboot</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入springboot最基础组件-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<!--引入springboot依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--定义springboot打包方式-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建控制类
package com.aaa.myspringboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping("/out")
@ResponseBody
public String out() {
return "success";
}
}
创建Spring Boot入口类
Spring Boot入口类:
- 入口类命名通常以*Application结尾
- 入口类增加@SpringBootApplication注解
- 利用SpringApplication.run()方法启动应用
package com.aaa.myspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//说明这是一个springboot应用的入口类
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
//启动SpringBoot应用
SpringApplication.run(MySpringBootApplication.class);
验证
- 运行MySpringBootApplication。并访问localhost:8080/out
Spring Initializr构建Spring Boot应用
2.
3.
启动流程
@Repository 对应DAO类,用于数据处理
@Service 对应业务逻辑类
@Controller 对应MVC的控制器类
@Component 对于某些不太好区分的类
Spring Boot常用配置
配置application.properties
server.port=80
server.servlet.context-path=/工程名
若不加server.servlet.context-path=/工程名。访问localhost/out。无需添加端口号
配置日志
server.port=80
server.servlet.context-path=/myspringboot
#指定日志保存路径
logging.file=D:/myspringboot.log
#debug-->info-->warn-->error-->fatal
#程序的调试日志-->程序一般性的输出信息-->警告信息,并不会引起程序的失败-->程序异常-->灾难性(服务器宕机)
logging.level.root=info
运行后会在D盘生成一个myspringboot.log日志文件
连接数据库
server.port=80
server.servlet.context-path=/myspringboot
#指定日志保存路径
logging.file=D:/myspringboot.log
#debug-->info-->warn-->error-->fatal
#程序的调试日志-->程序一般性的输出信息-->警告信息,并不会引起程序的失败-->程序异常-->灾难性(服务器宕机)
logging.level.root=info
debug=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/database
database
spring.datasource.username=aaa
spring.datasource.password=123
Spring Boot支持两种配置文件
- 属性文件:application.properties
- YAML格式: application.yml
YAML的语法
- YAML 是一种简洁的非标记语言。YAML以数据
为中心,使用空白,缩进,分行组织数据,从而使 得表示更加简洁易读。
YAML语法格式 - 标准格式:key:(空格)value
- 使用空格代表层级关系,以“:”结束
配置application.yml
- 创建src/resources/application.yml
server:
port: 80
logging:
level:
root: info
file:
name: "D:/myspringboot.log"
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost.com:3306/database
username: aaa
password: 123
Spring Boot自定义配置项
Spring Boot允许我们自定义应用配置项,在程序运
行时允许动态加载,这为程序提供了良好的可维护性。
在实际项目开发中,我们通常将项目的自定义信息放
在配置文件中。
- 在application.yml配置文件中自定义。eg:
mail:
config:
name: 冠兴
description: 商城
hot-sales: 10
show-advert: true
- 控制类:
package com.aaa.myspringboot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Value("${mail.config.name}")
private String name;
@Value("${mail.config.description}")
private String description;
@Value("${mail.config.hot-sales}")
private Integer hotSales;
@Value("${mail.config.show-advert}")
private Boolean showAdvert;
@RequestMapping("/out")
@ResponseBody
public String out() {
return "success";
}
@RequestMapping("/info")
@ResponseBody
public String info() {
return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s,", name, description, hotSales, showAdvert);
}
}
- 验证:localhost:8080/info
name:冠兴,description:商城,hot-sales:10,show-advert:true,
环境配置及切换
- Spring Boot可针对不同的环境提供不同的Profile文件。
- Profile文件的默认命名格式为application-{env}.yml
- 使用spring.profiles.active选项来指定不同的profile
- eg:
创建如下三个文件:
- dev(开发环境)
debug: true
logging:
level:
root: info
file:
name: "D:/myspringboot.log"
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/database
username: aaa
password: 123
mail:
config:
name: 冠兴(开发环境)
description: 商城
hot-sales: 10
show-advert: true
- prd(生产环境)
debug: false
logging:
level:
root: info
file:
name: "/local/user/app-prd.log"
server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.12.13.45:3306/database
username: aaa
password: 123
mail:
config:
name: 冠兴(生产环境)
description: 商城
hot-sales: 100
show-advert: true
- yml(切换环境)
spring:
profiles:
active: prd
这个时候访问localhost:8080/info
name:冠兴(生产环境),description:商城,hot-sales:10,show-advert:true,
打包与运行
-
利用Maven的package命令,生成可独立运行的Jar包。
-
利用 java -jar xxx.jar 命令启动Spring Boot应用。
-
Jar包可自动加载同目录的application配置文件。
- 通过修改application切换环境