欢迎来到 Kong Xiangqun 的博客

1、springboot新建项目\配置文件\配置文件优先级

一、新建项目

 

 

一个小东西

运行后输出的

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

可以更改

自定义banner

 新建banner.txt

 

bootschool.net/ascii

patorjk.com/software/taag/#p=display&f=Graffiti&t=Kuangshen

二、springboot配置文件

springboot打jar包运行方式

 点击package,会在target下看到jar包

Open in -> Show in Explorer 打开所在目录

通过java -jar 运行jar包

 

yaml配置文件可以看下面文档

docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config-yaml

 application.properties

server.port=8081

application.yaml

server:
  port: 8082

注意:

冒号后面的空格

两种配置文件的区别

 

 

application.yaml

server:
  port: 8082
person:
  name: zhangsan
  age: 12
  sex: 男
  likes:
    - book
    - movie
    - girl

把yaml文件的数据引入到.java文件

Person.java

package xyz.kxq.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@ConfigurationProperties(prefix="person")
@Component
public class Person {
    private String name;
    private Integer age;
    private String sex;
    private List<String> likes;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<String> getLikes() {
        return likes;
    }

    public void setLikes(List<String> likes) {
        this.likes = likes;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", likes=" + likes +
                '}';
    }
}

测试一下是否引入成功了

在test/java/xyz.kxq/StudyApplicationTests

package xyz.kxq;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import xyz.kxq.entity.Person;

@SpringBootTest
class StudyApplicationTests {

    @Autowired
    Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

}

运行后打印结果为:

/*
2021-09-29 09:37:47.636  INFO 53500 --- [           main] xyz.kxq.StudyApplicationTests            : Starting StudyApplicationTests using Java 1.8.0_301 on TJ-A-2106-005 with PID 53500 (started by koxixa in C:\kxq\E_file\java\project\springboot\study)
2021-09-29 09:37:47.638  INFO 53500 --- [           main] xyz.kxq.StudyApplicationTests            : No active profile set, falling back to default profiles: default
2021-09-29 09:37:49.257  INFO 53500 --- [           main] xyz.kxq.StudyApplicationTests            : Started StudyApplicationTests in 1.996 seconds (JVM running for 3.038)
Person{name='zhangsan', age=12, sex='男', likes=[book, movie, girl]}

Process finished with exit code 0
*/

 也可以用@Value注解, 麻烦

Person.java

package xyz.kxq.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

//@ConfigurationProperties(prefix="person")
@Component
public class Person {
    @Value("${person.name}")
private String name;
    @Value("${person.age}")
private Integer age;
    private String sex;
    private List<String> likes;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<String> getLikes() {
        return likes;
    }

    public void setLikes(List<String> likes) {
        this.likes = likes;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", likes=" + likes +
                '}';
    }
}

运行测试代码

打印结果为:

2021-10-14 14:24:40.991  INFO 6412 --- [           main] xyz.kxq.StudyApplicationTests            : Starting StudyApplicationTests using Java 1.8.0_301 on TJ-A-2106-005 with PID 6412 (started by koxixa in C:\kxq\E_file\java\project\springboot\study)
2021-10-14 14:24:40.994  INFO 6412 --- [           main] xyz.kxq.StudyApplicationTests            : No active profile set, falling back to default profiles: default
2021-10-14 14:24:42.407  INFO 6412 --- [           main] xyz.kxq.StudyApplicationTests            : Started StudyApplicationTests in 1.766 seconds (JVM running for 2.788)
Person{name='zhangsan', age=12, sex='null', likes=null}

 三、配置文件优先级

/*
file:./config/

file:./

classpath:/config/

classpath:/
*/

file 当前文件路径

classpath resources下有application.yaml resources目录相当于classpath

 此时运行程序

 

 端口为8081

把file:./config/ 删掉重新运行

 

 

 端口为8082

把file:./ 删掉重新运行

 

 端口为8082 

把classpath:/config/ 删掉重新运行

 

  端口为8084

 

一般情况都是在classpath写配置文件, 如果配置文件比较多就放在classpath:/config/

 

posted @ 2021-10-14 16:39  kongxiangqun20220317  阅读(156)  评论(0编辑  收藏  举报