springboot - 配置文件
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配置注解执行器没有配置”

浙公网安备 33010602011771号