【第2章】SpringBoot配置文件

文章目录


前言

SpringBoot工程创建后,会为我们提供一个默认的配置文件(application.properties),配置文件主要用于那些可能发生变化且经常改变的属性值。


一、编写配置信息

SpringBoot为我们提供了很多的内置配置信息,如:server.port等,如果我们在配置文件中定义了属性值则会覆盖默认值,未声明则使用默认值;

我们也可以自定义属性值。
主要的配置文件又分为properties和yml两种

1. properties

properties以键值对的形式存在。

bash 复制代码
#内置配置
spring.application.name=spring-boot3
server.port=8080
#自定义配置
#prop.ip=127.0.0.1
prop.port=80
prop.user=admin
prop.pwd=123456
#数组
prop.singer=ChanteMoore,凡希亚,那英

IDEA使用properties配置文件需要配置ascii转换,否则会乱码 确保IDE和JVM都配置为使用UTF-8编码,可以添加 -Dfile.encoding=UTF-8 参数。

2. yml

bash 复制代码
#内置配置
spring:
  application:
    name: spring-boot3
server:
  port: 8081
#自定义配置
yml:
  #ip: 127.0.0.1
  port: 80
  user: admin
  pwd: 123456
  #数组
  singer: ChanteMoore,凡希亚,那英

application.properties优先级更高

二、获取配置信息

1.直接获取

java 复制代码
package org.example.springboot3.controller;

import org.example.springboot3.config.SpringBootConfig;
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;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by zjg on 2024/5/17
 */
@RestController
@RequestMapping("/config/")
public class ConfigController {
    @Value("${prop.ip:127.0.0.1}")
    private String ip;
    @Value("${prop.port:80}")
    private int port;
    @Value("${prop.user}")
    private String user;
    @Value("${prop.pwd}")
    private String pwd;
    @Value("${prop.singer}")
    private String []singer;
    //直接获取
    @RequestMapping("001")
    public String config001(){
        Map<String,Object> map=new HashMap<>();
        map.put("ip",ip);
        map.put("port",port);
        map.put("user",user);
        map.put("pwd",pwd);
        System.out.println(Arrays.toString(singer));
        System.out.println(map);
        return map.toString();
    }
    @Autowired
    SpringBootConfig springBootConfig;
    //配置类形式
    @RequestMapping("002")
    public String config002(){
        System.out.println(springBootConfig);
        return springBootConfig.toString();
    }
}

ChanteMoore, 凡希亚, 那英

{port=80, ip=127.0.0.1, pwd=123456, user=admin}

2.配置类形式

java 复制代码
package org.example.springboot3.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;

/**
 * Create by zjg on 2024/5/17
 */
@Configuration
@ConfigurationProperties(prefix = "yml")
public class SpringBootConfig {
    @Value("${yml.ip:127.0.0.1}")
    private String ip;
    private int port;
    private String user;
    private String pwd;
    private String []singer;

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setSinger(String[] singer) {
        this.singer = singer;
    }

    @Override
    public String toString() {
        return "SpringBootConfig{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", user='" + user + '\'' +
                ", pwd='" + pwd + '\'' +
                ", singer=" + Arrays.toString(singer) +
                '}';
    }
}

SpringBootConfig{ip='127.0.0.1', port=80, user='admin', pwd='123456', singer=[ChanteMoore, 凡希亚, 那英]}


总结

回到顶部
更多内置属性
Properties和Yaml在线转换

properties语法更加直观,但键容易重复。

yml配置文件语法更加简洁,层级更加清晰。

相关推荐
张较瘦_4 分钟前
[论文阅读] 软件工程 | 告别“线程安全玄学”:基于JMM的Java类静态分析,CodeQL3分钟扫遍GitHub千仓错误
java·论文阅读·安全
A尘埃2 小时前
智慧零售全渠道业务中台系统
java·零售
尘鹄2 小时前
go 初始化组件最佳实践
后端·设计模式·golang
墩墩分墩2 小时前
【Go语言入门教程】 Go语言的起源与技术特点:从诞生到现代编程利器(一)
开发语言·后端·golang·go
一个松4 小时前
【无标题】
spring boot
程序员爱钓鱼4 小时前
Go语言实战案例- 开发一个ToDo命令行工具
后端·google·go
小wanga5 小时前
C++知识
java·开发语言·c++
学渣676565 小时前
文件传输工具rsync|rust开发环境安装|Ascend实验相关命令
开发语言·后端·rust
我是渣哥5 小时前
Java String vs StringBuilder vs StringBuffer:一个性能优化的探险故事
java·开发语言·jvm·后端·算法·职场和发展·性能优化