首页 > 编程语言 > 详细

springboot - 配置文件

时间:2021-03-27 18:42:53      阅读:33      评论:0      收藏:0      [点我收藏+]
springboot配置文件方式1:properties【application.properties是系统配置文件,自定义文件用法类似】
  • 默认配置文件 application.properties,一般放置系统配置
  • 放置的目录
    • 当前项目根目录下的 config 目录下
    • 当前项目的根目录下
    • resources 目录下的 config 目录下
    • resources 目录
  • 四个配置文件的优先级一次降低,这四个位置是默认位置,既Spring Boot启动,默认会从这四个位置按顺序去查找相关属性并加载。

技术分享图片

 

  • 系统配置文件的使用

技术分享图片

 

  •  代码
package com.gongxy.demo.config;

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

/*
 * prefix 里面不能用驼峰写法
 */
@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "setting")
public class StudyConfig {
    private String companyNode;

    public String getCompanyNode() {
        return companyNode;
    }

    public void setCompanyNode(String companyNode) {
        this.companyNode = companyNode;
    }
}

技术分享图片

 

 技术分享图片

springboot配置文件方式2:yaml
  • yaml文件

技术分享图片

 

 

  • 数组注入
my:
  servers:
    - dev.example.com
    - another.example.com
  • 绑定
package com.gongxy.demo.config;

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

import java.util.List;

@Component
@ConfigurationProperties(prefix = "my")
public class YamlConfig {
    private List<String> servers;

    public List<String> getServers() {
        return servers;
    }

    public void setServers(List<String> servers) {
        this.servers = servers;
    }
}
  • 储存对象
redis:
  redisConfigs:
    - host: 192.168.66.128
      port: 6379
    - host: 192.168.66.129
      port: 6380
  • 绑定
package com.gongxy.demo.config;

public class SingleRedisConfig {
    private String host;
    private Integer port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

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

    @Override
    public String toString() {
        return "SingleRedisConfig{" +
                "host=‘" + host + ‘\‘‘ +
                ", port=" + port +
                ‘}‘;
    }
}

  

package com.gongxy.demo.config;

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

import java.util.List;

@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
    public List<SingleRedisConfig> getRedisConfigs() {
        return redisConfigs;
    }

    public void setRedisConfigs(List<SingleRedisConfig> redisConfigs) {
        this.redisConfigs = redisConfigs;
    }

    private List<SingleRedisConfig> redisConfigs;
}

  

 错误1:Spring Boot Configuration Annotation Processor not configured 问题解决

 技术分享图片

 

  •  问题解决方案,在pom.xml文件中引入依赖,问题即可解决
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

 

  • 问题分析:它的意思是“Spring Boot配置注解执行器没有配置”

 

 

 

springboot - 配置文件

原文:https://www.cnblogs.com/gygtech/p/14586071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!