遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

SpringBoot配置文件详解

简介

SpringBoot全局配置文件默认为src/main/resources下的application.properties,后缀可以改为yml,

如果application.yml和application.properties两个配置文件都存在,那么,properties优先级更高

官网(Spring Boot 全部配置项):https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties

注意:配置文件改了要重启

 

修改默认配置

修改端口

application.properties

1
server.port=9080

  

application.yml

1
2
server:
  port: 9080

 

自定义属性配置

在SpringBoot配置文件中自定义属性配置,等号左侧的key可以随便写,但是要和其它配置区分开,要且见名知意

application.properties

1
2
3
com.qzcsbj.student.name=jack
com.qzcsbj.student.age=18
com.qzcsbj.student.desc=boy

 

实体类:注意,下面没有setter方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.qzcsbj.demo.bean;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Component
public class Student {
    @Value("${com.qzcsbj.student.name}")
    private String name;
    @Value("${com.qzcsbj.student.age}")
    private Integer age;
    @Value("${com.qzcsbj.student.desc}")
    private String desc;
 
    public Student() {
        System.out.println("=========调用实体类无参构造");
    }
 
    public String getName() {
        return name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public String getDesc() {
        return desc;
    }
 
 
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", desc='" + desc + '\'' +
                '}';
    }
}

  

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.qzcsbj.demo.controller;
 
import com.qzcsbj.demo.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RestController
public class StudentController {
 
    public StudentController() {
        System.out.println("=========控制器构造方法");
        System.out.println("=========控制器中@Autowired注入的student:" + student);
    }
 
    @Autowired
    Student student;
 
    @RequestMapping("getInfo2")
    public Student getInfo2(){
        System.out.println("=====控制器的方法中student:" + student);
        return student;
    }
}

 

启动项目后,请求:localhost:8080/getInfo2

日志

 

如果是用非全局配置文件:student.properties

1
2
3
com.qzcsbj.student.name=lucy
com.qzcsbj.student.age=${random.int(1,100)}
com.qzcsbj.student.desc=girl

 

需要指定配置文件:

@PropertySource("classpath:student.properties")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.qzcsbj.demo.bean;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Component
@PropertySource("classpath:student.properties")
public class Student {
    @Value("${com.qzcsbj.student.name}")
    private String name;
    @Value("${com.qzcsbj.student.age}")
    private Integer age;
    @Value("${com.qzcsbj.student.desc}")
    private String desc;
 
    public Student() {
        System.out.println("=========调用实体类无参构造");
    }
 
    public String getName() {
        return name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public String getDesc() {
        return desc;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", desc='" + desc + '\'' +
                '}';
    }
}

 

请求:localhost:8080/getInfo2

 

将属性配置赋值给实体类

当我们属性配置很多的时候,使用@Value注解一个一个的注入将会变得很繁琐,这时SpringBoot提供了将属性配置与实体类结合的方式

resources下:application.properties

1
2
3
com.qzcsbj.student.name=jack
com.qzcsbj.student.age=18
com.qzcsbj.student.desc=boy

 

创建一个配置类,必须要有set方法,因为底层通过set方法注入的

以com.qzcsbj.student开头的属性,下面和配置文件中名字一样的,会自动装配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.qzcsbj.config;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component  // 先生成对象
@ConfigurationProperties(prefix = "com.qzcsbj.student")
public class StudentProperties {
    private String name;
    private Integer age;
    private String desc;
}

 

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.qzcsbj.demo.controller;
 
import com.qzcsbj.demo.bean.Student;
import com.qzcsbj.demo.config.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RestController
public class StudentController {
    public StudentController() {
        System.out.println("=========控制器构造方法");
        System.out.println("=========控制器中@Autowired注入的student:" + student);
    }
 
    @Autowired  // 注入对象
    StudentProperties studentProperties;<br>
    @RequestMapping("getInfo3")
    public StudentProperties getInfo3(){
        return studentProperties;
    }
}

 

请求:localhost:8080/getInfo3

 

自定义配置文件

有时我们需要自定义配置文件,将上文关于student的属性配置提取到student.properties文件中

1
2
3
com.qzcsbj.student.name=lucy
com.qzcsbj.student.age=${random.int(1,100)}
com.qzcsbj.student.desc=girl

 

配置类:需要@PropertySource指定配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.qzcsbj.demo.config;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component  // 先生成对象
@PropertySource("classpath:student.properties")
@ConfigurationProperties(prefix = "com.qzcsbj.student")
public class StudentProperties {
    private String name;
    private Integer age;
    private String desc;
}

 

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.qzcsbj.demo.controller;
 
import com.qzcsbj.demo.bean.Student;
import com.qzcsbj.demo.config.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RestController
public class StudentController {
 
    public StudentController() {
        System.out.println("=========控制器构造方法");
        System.out.println("=========控制器中@Autowired注入的student:" + student);
    }
 
    @Autowired  // 注入对象
    StudentProperties studentProperties;<br>
    @RequestMapping("getInfo3")
    public StudentProperties getInfo3(){
        return studentProperties;
    }
}

 

请求:http://localhost:8080/getInfo3

 

多环境配置

实际开发中可能会有不同的环境,他们配置也不一样,如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties或者application-{profile}.yml的格式,其中{profile}对应你的环境标识,

比如:

application-dev.yml:开发环境

1
2
server:
  port: 8081

  

application-test.yml:测试环境

1
2
server:
  port: 8082

 

application-prod.yml:生产环境

1
2
server:
  port: 8083

 

激活profile,在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值,如:

1
2
3
spring:
  profiles:
    active: dev

 

启动项目,可以看到端口是dev的端口

 

也可以用:application.properties

1
spring.profiles.active=test

 

重新启动项目,可以看到端口是test的端口

 

【bak】

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17064508.html

 

posted @   全栈测试笔记  阅读(828)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2021-01-22 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
2021-01-22 宿主机ping不通虚拟机,虚拟机能ping通宿主机
2020-01-22 【V】自动化必备:自动化持续集成环境搭建(下):git + maven + jenkins
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示