properties文件

设置启动端口

  1. server.port=8090

绑定bean的方式获取配置文件中的内容

  1. @Configuration//声明当前类为配置文件类
  2. @ConfigurationProperties(prefix = "foot") //设置配置文件前缀
  3. @EnableConfigurationProperties({FootConfig.class})//引入配置文件
  4. @PropertySource("classpath:vegetables.properties")//配置文件地址-不是application就要去找
  1. #properties配置文件
  2. #如果配置文件中给中文要在settings中设置:搜encoding--file encodings--
  3. #Project Encoding 设成UTF-8
  4. #properties Files 设成UTF-8 并且将后面的选框选上
  5. foot.rice=米饭
  6. foot.meat=烤肉
//java
引入依赖
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
 </dependency>
 //创建配置文件类
@ConfigurationProperties(prefix = "foot") //设置前缀前缀
public class FootConfig {
    private String rice;//配置文件中的后缀会被当做属性
    private String meat;
    //必须有get set方法
}
//主程序入口类引入配置文件
@EnableConfigurationProperties({FootConfig.class})
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
//使用
@Autowired
    FootConfig config;
    @RequestMapping("foot")
    public Foot foot() {
        Foot foot = new Foot();
        foot.setRice(config.getRice());
        foot.setMeat(config.getMeat());
        return foot;
    }
//以上的基础上再添加一个配置类
vegetables.properties
    vegetables.potato=土豆
    vegetables.eggplant=茄子
    vegetables.greenpeper=青椒
//配置类
@Configuration//不是application名字要加上
@ConfigurationProperties(prefix = "vegetables")
@PropertySource("classpath:vegetables.properties")//不是application名字要加上
@Data
public class VegetablesConfig {
    private String potato;
    private String eggplant;
    private String greenpeper;
}
//主程序入口类
@EnableConfigurationProperties({FootConfig.class, VegetablesConfig.class})//多个配置文件用,隔开
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
//使用同上

YAML

语法规则
大小写敏感
使用缩进表示层级关系
缩进是不可使用TAB 只可以空格
Springboot默认的文件是application.yml
设置启动端口

server:
  port: 8090

绑定bean的方式获取配置文件中的内容

#application.yml
foot:
  rice: 米饭
  meat: 烤肉
#yml定义数组
  sauce:
    - 咖喱
    - 蜜汁
    - 原味 #- 值 的形式定义数组
//java--用法和properties文件一样
引入依赖
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
 </dependency>
 //创建配置文件类
@ConfigurationProperties(prefix = "foot") //设置前缀前缀
public class FootConfig {
    private String rice;//配置文件中的后缀会被当做属性
    private String meat;
    private String[] sauce;//获取yml配置文件中设置的数组
    //必须有get set方法
}
//主程序入口类引入配置文件
@EnableConfigurationProperties({FootConfig.class})
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
//使用
@Autowired
    FootConfig config;
    @RequestMapping("foot")
    public Foot foot() {
        Foot foot = new Foot();
        foot.setRice(config.getRice());
        foot.setMeat(config.getMeat());
        foot.setSauce(config.getSauce());
        return foot;
    }