SpringBoot内部配置文件 .properties, .yml, .yaml

简单快速的入门和使用,不涉及过多讲解。

配置文件后缀的三种形式

        application.properties, application.yml,application.yaml

加载优先级是 application.properties > application.yml > application.yaml,项目里选择一个使用。

可存放的四个位置

(默认是放在resources的目录下的)

 1. A /config subdirectory of the current directory  --- 当前目录的 /config 子目录
 2. The current directory  ------------------------------------当前目录
 3. A classpath /config package  ---------------------------resources目录的/config 子目录        
 4. The classpath root  ----------------------------------------resources目录

四个位置读取的优先级就是上面列出的顺序

application.properties 的书写形式
server.port=8089
server.servlet.context-path=/admin
spring.datasource.url=jdbc:mysql://localhost:3306/example?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
application.yml或.yaml的书写形式

注意:key和value之间必须有一个空格,一定注意层级关系,层级不对会导致读不到属性

server:
  port: 8080
  servlet:
    context-path: "/admin" #修改项目访问路径 ,默认是 /

  #mysql
  datasource:
    url: jdbc:mysql://localhost:3306/example?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
   配置文件的读取方式
  1.    @Value
  2.  Environment
  3. @ConfigurationProperties  

application.yml 【注意value与“:”之间必须有空格 】

language: Chinese
#对象
person:
  name: Elis
  age: 18
  language: ${language} #引用language配置的值
#数组
city:
  - 北京
  - 上海
  - 广州
#纯元量
message1: 'Hello \n everyone' #单引号下 \n 会原样输出
message2: "Hello \n everyone" #双引号下 \n 会换行
1、@Value 

PrepertiesAttribution.java , 使用@Value读取配置文件中的属性

/**
 * 读取配置文件属性
 */
@Data
public class PropertiesAttribution {
   //对象的读取方式
    @Value("${person.name}")
    private String name;
    @Value("${person.age}")
    private Integer age;
    @Value("${person.language}")
    private String language;
    //数组的读取方式
    @Value("${city[0]}")
    private String city1;
    //纯元量的读取方式
    @Value("${message1}")
    private String message1;
    @Value("${message2}")
    private String message2;


    @Override
    public String toString() {
        return "PropertiesAttribution{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", language='" + language + '\'' +
                ", city1='" + city1 + '\'' +
                ", message1='" + message1 + '\'' +
                ", message2='" + message2 + '\'' +
                '}';
    }

}

验证一下结果是否正确

AppConfig.java, 用于创建各种bean(不想写一个文件来创建bean的话,可以直接在PrepertiesAttribution.java加上@Component注解)

@Configuration
public class AppConfig {
    @Bean
    public PropertiesAttribution attribution() {
        return new PropertiesAttribution();
    }
}

    VolcanoAdminApplication.java, Springboot项目的启动类,在这里获取一下PropertiesAttribution的Bean对象,看一下@Value获取到的各个属性值

@SpringBootApplication
public class VolcanoAdminApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(VolcanoAdminApplication.class, args);
		PropertiesAttribution attr = context.getBean(PropertiesAttribution.class);
		System.out.println(attr);
	}

}

   输出结果:

    

2、Environment 

依然使用PropertiesAttribution.java

PropertiesAttributionTest.java, 测试类中读取一下

@RunWith(SpringRunner.class)
@SpringBootTest(classes = VolcanoAdminApplication.class)
public class UserServiceTest {

    @Autowired
    Environment env;

    @Test
    public void test() {
        PropertiesAttribution attribution = new PropertiesAttribution();
        attribution.setName(env.getProperty("person.name"));
        attribution.setAge(Integer.valueOf(env.getProperty("person.age")));
        attribution.setLanguage(env.getProperty("person.language"));
        attribution.setCity1(env.getProperty("city[0]"));
        attribution.setMessage1(env.getProperty("message1"));
        attribution.setMessage2(env.getProperty("message2"));
        System.out.println(attribution);

    }

}

输出结果:

3、@ConfigurationProperties

在PropertiesAttribution.java中加上@ConfigurationProperties(prefix = "person"), 【前缀prefix = "person"的意义是指定读取范围】

@Data
@ConfigurationProperties(prefix = "person")
public class PropertiesAttribution { 
    private String name;
    private Integer age;
    private String language;
    private String city1;
    private String message1;
    private String message2;

    @Override
    public String toString() {
        return "PropertiesAttribution{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", language='" + language + '\'' +
                ", city1='" + city1 + '\'' +
                ", message1='" + message1 + '\'' +
                ", message2='" + message2 + '\'' +
                '}';
    }

加上前缀 prefix = "person"

 不加前缀 

 language可以读到是因为外部有定义 language: Chinese

profile多环境配置文件

实际开发项目中,会涉及到多个环境,如 dev(开发环境),test(测试环境),pro(线上环境),不同环境下使用的配置会有所不同,比如端口号、数据库地址、redis地址不同等等,就需要可以动态配置不同的配置文件,为了减少篇幅,具体实现写在另一个文档里

springboot设置多环境配置文件

外部配置文件

springboot 也支持执行在执行java -jar xxx.jar命令时指定外部配置文件,作为内部文件的一个补充。可以替换内部文件的属性值。官方文档:Externalized Configuration :: Spring Boot

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值