一、SpringBoot起步原理原理
- 在spring-boot-starter-parent中定义了各种技术的版本信息,组合了一套最优搭配的技术版本
- 在各种starter中,定义了完成该功能需要的坐标集合,其中大部分版本信息来自于父工程
- 我们的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,并且不会存在版本冲突等问题。
二、SpringBoot配置
为了方便比较我们在 src/main/java/resources文件下新建application.yml和application.yaml
首先右击resources文件——》点击New——》Directory——》输入名称application.yml
application.yaml也是同样的步骤
如图所示即可
现在我们已知这三种配置文件的格式分别为: properties、yaml、yml
YAML
YAML全称为YAML Ain't Markup Language.YAML是一种直观的能够被电脑识别的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互,可以被支持YAML库的不同的编程语言程序导入,比如:C/C++,Ruby,Python,java,Perl,C#,PHP等,YML文件是以数据为核心的,比传统的xml方式更加简洁。
YAML文件的拓展名可以选用.yml或者.yaml.
perperties文件中的书写格式
#properties书写格式
server.port=8080
server.address=127.0.0.1
在xml文件中的书写格式
<server>
<port>8080</port>
<address>127.0.0.1</address>
</server>
在yml文件中的书写格式
#yml书写格式
server:
port: 8080
address: 127.0.0.1
YAML基本语法
YAML:数据格式
YAML:小结
三、SpringBoot读取配置文件内容
我们在application.properties文件中写入如下代码
#properties书写格式
server.port=8080
在application.yml文件中写入
#yml书写格式
server:
port: 8081
在application.yaml文件中写入
server:
port: 8082
此时运行项目,访问localhost:8080/hellohttp://localhost:8080/hello
所占用的端口号为8080
我们把application.propertie文件中的配置代码注销或者删除。
在运行项目,我们这个时候的我们访问是无法访问的localhosthttp://localhost:8080/hello
如果将服务地址改为localhost:8081/hellohttp://localhost:8081/hello
则可以继续访问。
如果我们将application.yml文件中的配置代码注销或者删除。
在运行项目,我们这个时候的我们访问localhost:8081/hello是无法访问的
需要将访问地址改为:localhost:8082/hello就可以访问了;
所以我们可以得出在同一级目录下优先级:properties>yml>yaml
读取配置内容的方式:
三种方式:
@Value
Environment
@ConfigurationProperties
为了方便测试我们接下来的配置信息只在application.yml文件中写入
#yml书写格式
server:
port: 8081
person:
name: zhangsan
age: 20
address:
-beijing
-shanghai
1.使用@Value访问数据
HelloController.java文件中的代码
package com.example.myspringboothello2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private String age;
@Value("${person.address}")
private String [] address;
@RequestMapping("/hello2")
public String hello2(){
System.out.println("我们的名字是:"+name+"我的年龄是:"+age);
String[] address = this.address;
for (String s : address) {
System.out.println(s);
}
return "你好 我是SpringBoot的Hello2";
}
application.yml文件中的代码:
#yml书写格式
server:
port: 8081
#对象
person:
name: zhangsan #${name}#参数引用
age: 20
address:
-beijing
-shanghai
运行项目,访问地址:localhost:8081/hello2
运行结果:
2.使用Environment访问配置数据
HelloController.java文件中的代码
package com.example.myspringboothello2;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private Environment evm;
@RequestMapping("/hello3")
public String hello3(){
System.out.println("姓名:"+evm.getProperty("person.name"));
System.out.println("年龄:"+evm.getProperty("person.age"));
return "你好 我是SpringBoot的Hello3";
}
}
运行项目,访问地址:localhost:8081/hello2
运行结果:
3.我们在myspringboothello2包新建实体类Person
package com.example.myspringboothello2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private String age;
private String[]address;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
}
此时Person类上面会爆红,我们需要将以下依赖拷贝进pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
pom.xml文件代码如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mySpringBoot-Hello2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mySpringBoot-Hello2</name>
<description>mySpringBoot-Hello2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在HelloController文件中写入
package com.example.myspringboothello2;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private String age;
@Value("${person.address}")
private String [] address;
@Autowired
private Environment evm;
@Autowired
private Person person;
@RequestMapping("/hello4")
public String hello4(){
System.out.println(person.getName()+person.getAge());
String[] address = person.getAddress();
for (String s : address) {
System.out.println(s);
}
return "你好 我是SpringBoot的Hello4";
}
@RequestMapping("/hello3")
public String hello3(){
System.out.println("姓名:"+evm.getProperty("person.name"));
System.out.println("年龄:"+evm.getProperty("person.age"));
return "你好 我是SpringBoot的Hello3";
}
@RequestMapping("/hello2")
public String hello2(){
System.out.println("我们的名字是:"+name+"我的年龄是:"+age);
String[] address = this.address;
for (String s : address) {
System.out.println(s);
}
return "你好 我是SpringBoot的Hello2";
}
@RequestMapping("/hello")
public String hello(){
return "你好 我第二个是SpringBoot项目";
}
}
运行项目,在游览器访问:localhost:8081/hello4
运行效果为:
配置文件分析:
配置文件分类:
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.propertion或者application.yml(application.yaml)进行配置。
- properties:(键值对的形式配置)
Server.port=8080
- Yml:(表示port属于server父类的属性)
Server:
Port:8080
小结
- SpringBoot提供了2种配置文件类型:properties和yml/yaml
- 默认配置文件名称:application
- 在同一级目录下优先级:properties>yml>yaml