Spring Boot Learning(配置文件)

Properties配置

配置文件的生效顺序,会对值进行覆盖:

1. @TestPropertySource 注解
2. 命令行参数
3. Java系统属性(System.getProperties())
4. 操作系统环境变量
5. 只有在random.*里包含的属性会产生一个RandomValuePropertySource
6. 在打包的jar外的应用程序配置文件(application.properties,包含YAML和profile变量)
7. 在打包的jar内的应用程序配置文件(application.properties,包含YAML和profile变量)
8. 在@Configuration类上的@PropertySource注解
9. 默认属性(使用SpringApplication.setDefaultProperties指定)

配置随机值:

eric.secret=${random.value}
eric.number=${random.int}

读取使用注解:@Value(value = "${eric.secret}")

注:出现黄点提示,是要提示配置元数据,可以不配置

 

属性占位符:

当application.properties里的值被使用时,它们会被存在的Environment过滤,所以你能够引用先前定义的值(比如,系统属性)。
eric.name=www.eric.com
eric.desc=${eric.name} is a domain name

 

Application属性文件,按优先级排序,位置高的将覆盖位置低的

1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个classpath下的/config包
4. classpath根路径(root)

这个列表是按优先级排序的(列表中位置高的将覆盖位置低的),例如在resource新建config子目录并在这个目录下生成application.properties ,它会覆盖resource下的application.properties文件(如果文件中有相同属性)。

 

配置应用端口和其他配置的介绍:

#端口配置:
server.port=8090


#时间格式化

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

#时区设置
spring.jackson.time-zone=Asia/Chongqing

 *注意:默认时区是UTC,需要更改为当前时区

 

Yaml配置文件(建议使用它代替properties文件)

代码:

package com.example.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.bean.User;

@RestController
@RequestMapping(value = "index")
public class IndexController {

    @Value(value = "${eric.secret}")
    private String secret;
    
    @Value(value = "${eric.number}")
    private int number;
    
    @Value(value = "${eric.desc}")
    private String desc;
    
    
    @RequestMapping
    public String index() {
        return "hello spring boot!";
    }

    @RequestMapping(value = "get")
    public Map<String, Object> get(@RequestParam String name) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("value", "hello boot");
        map.put("secret", secret);
        map.put("number", number);
        map.put("desc", desc);
        
        return map;
    }

    @RequestMapping(value = "find/{id}/{name}")
    public User get(@PathVariable int id, @PathVariable String name) {

        User user = new User();
        
        user.setId(id);
        user.setName(name);
        user.setDate(new Date());

        return user;
    }
}

 

application.properties:

#随机数,只有在重启后值才会有变化
#32位的随机数
eric.secret=${random.value}
eric.number=${random.int}

eric.name=www.eric.com
eric.desc=${eric.name} is new

application.yaml:

#随机数,只有在重启后值才会有变化
#32位的随机数
eric:
  secret: ${random.value}
  number: ${random.int}
  name: www.eric.com
  desc: ${eric.name} is a domain name

server:
  port: 9090

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss

 

代码目录结构:

 

 

posted @ 2017-03-11 14:30  lys0410  阅读(333)  评论(0编辑  收藏  举报