1. Springboot框架
1.1 什么是springboot
Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。
1.2 springboot优点
① 创建独立的 Spring 应用程序
② 嵌入的 Tomcat,无需部署 WAR 文件
③ 简化 Maven 配置
④ 自动配置 Spring
⑤ 开箱即用,没有代码生成,也无需 XML 配置。
1.3springboot快速入门
1.准备条件
(1)JDK 环境必须是 1.8 及以上
(2)后面要使用到 Maven 管理工具 3.2.5 及以上版本.
(3)开发工具建议使用 IDEA,
2.创建一个springboot工程
2.1使用idea的自动创建
2.2在现有maven项目中加入spring boot
在pom.xml中使用父继承
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
导入需要的jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
插件供需要使用
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
开始使用spring boot
测试类下运行
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意!:开启spring boot只能在该测试类等级以下的类中运行,平级也不可以!
正确:
错误:
1.4 springboot的两种配置文件
application.properties属性文件
application.yml文件
properties形式
#基础存储
student.name=
student.age=
student.address=
#数组形式
student.gooby[0]=
student.gooby[1]=
#map形式
student.map.name=
student.map.age=
yml形式
#yml文件区分上下级,一个空格即视为下级
student:
name:
age:
sex:
map:
name:
age:
1.5 java类读取配置文件的内容
(1)//第一种通过ConfigurationProperties 来读取相关的配置内容
(2)第二种读取配置文件的内容
这种方式只能读取基本类型以及字符串类型。
1.6 profiles配置详解
为什么要使用profiles
在开发中,一般有两种环境
1,生产环境 [项目上线,客户在使用中,就是生产环境]
2,开发环境[就是开发环境,不解释]
有时候开发环境和生产环境的配置方法是不一样的,那么如何快速的切换呢,这里就要使用profiles文件
使用方式: