官方文档
Sping Boot Reference Guide
#更改项目的端口号
server.port=8081
#springboot这个配置文件中到底可以配置哪些东西呢
#方法一,官方的配置太多了
#方法二,了解原理,一通百通
配置文件
Sprongboot使用一个全局的配置文件,配置文件名称是固定的
- application.properties
- 语法结构:key=value
- application.yml
- 语法结构:key:空格 value
yaml配置:
server:
prot: 8080
server:
port: 8081
#对空格的要求非常严格
#普通的key-value
#注入到我们的配置类中!
name: lina
#对象
student:
name: lina
age: 3
#行内写法
student: {name: lina,age: 3}
# 数组
pets:
- cat
- dog
- pig
pets: [cat,dog,pig]
xml配置:
<server>
<port>8081<port>
</server>
给属性赋值的几种方式
第一种赋值方式
在yaml直接配置数据:
person:
name: lina
age: 22
happy: flase
birth: 1999/05/05
maps: {k1: v1,k2: v2}
lists:
-code
-music
-sleep
dog:
name: 布丁
age: 1
可以通过这个配置产生提示
@ConfigurationProperties
- Dog
package com.na.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Dog {
private String name;
private String age;
public Dog() {
}
public Dog(String name, String age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "Dog{" +
"namel='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
- Person
package com.na.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
@ConfigurationProperties作用:
将配置文件中配置每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件进行绑定
参数 prefix = "person" : 将配置文件中的person下面的所有属性一一对应
只有这个组件是容器中的组件,才能使用容器提供@ConfigurationProperties功能
*/
@Component //注册bean
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private String age;
private String happy;
private Date birth;
private Map<String,Object>maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public Person(String name, String age, String happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
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 getHappy() {
return happy;
}
public void setHappy(String happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", happy='" + happy + '\'' +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
- test
package com.na;
import com.na.pojo.Dog;
import com.na.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot01HelloworldApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
第二种赋值方式
在.properties中赋值
name=lina
1.Person
package com.na.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component //注册bean
//@ConfigurationProperties(prefix = "person")
@PropertySource(value = "classpath:application.properties")
public class Person {
//SPEL表达式取出配置文件的值
@Value("${name}")
private String name;
private String age;
private String happy;
private Date birth;
private Map<String,Object>maps;
private List<Object> lists;
private Dog dog;
public Person() {
}
public Person(String name, String age, String happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
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 getHappy() {
return happy;
}
public void setHappy(String happy) {
this.happy = happy;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", happy='" + happy + '\'' +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
2.test
package com.na;
import com.na.pojo.Dog;
import com.na.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot01HelloworldApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
多环境配置以及配置文件位置
文件位置
- file:./config/
- file:./
- classpath:/config/
- classpath:/
多环境配置
"—"来分割
server:
port: 8081
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
---
server:
port: 8084