配置pom.xml文件
官网获取https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/getting-started-installing-spring-boot.html#getting-started-maven-installation
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>springbootartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
书写一个控制类
package com.controll;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class hellocontroller {
@RequestMapping("/hello")
public String hello(){
return "hello spring boot";
}
}
书写一个引导类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class helloapplication {
public static void main(String[] args) {
SpringApplication.run(helloapplication.class,args);
}
}
在引导类文件中启动项目。
https://www.bilibili.com/video/BV1Lq4y1J77x?p=5&vd_source=fce42173caa30698e3df32059b6f9232
简洁,以数据为核心
server:
port: 8082
name: abc
#对象
person:
name: zhangsan
age: 20
#对象行内写法
person2: {name: zhangsan,age: 20}
#数组
address:
- beijing
- shanghai
#数组行内写法
address2: [beijing,shanghai]
#纯量
msg1: 'hello \n world' #不会识别转义字符,会原样输出
msg2: "hello \n world" #会识别转义字符
@RestController
public class hellocontroller {
@Value("${name}")//第一种方法
private String name;
@Value("${msg1}")//纯量
private String msg1;
@Value("${msg2}")
private String msg2;
@Value("${person.name}")
private String name2;
@Value("${address[0]}")//数组
private String addresss;
@Value("${person.age}")
private int age;
@Autowired//第二种方法
private Environment env;
@Autowired//第三种方法
private person person;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(addresss);
System.out.println(msg1);
System.out.println(msg2);
System.out.println(name2);
System.out.println(age);
System.out.println("------------------------");
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[0]"));
System.out.println("---------------------");
System.out.println(person);
return "hello spring boot";
}
@RequestMapping("/hello")
public String hello(){
return "hello spring boot";
}
}
perosn:
@Component
@ConfigurationProperties(prefix="person")
public class person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
激活后缀名为pro的配置文件
--- #可以分割为不同几个区域
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
---
server:
port: 8083
spring:
profiles: pro
---
spring:
profiles:
active: pro
虚拟机和程序参数也可以激活
虚拟机:-Dspring.profiles.active=pro
程序参数:--spring.profiles.active=test
视频链接
通过官网查看优先级
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-external-config.html
链接: 视频链接