springboot-4-配置文件读取

1. SpringBoot2.x常见的配置文件

SpringBoot2.x常见的配置文件 xml、yml、properties的区别和使用
YAML(Yet Another Markup Language)
写 YAML 要比写 XML 快得多(无需关注标签或引号)
使用空格 Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目 注意:key后面的冒号,后面一定要跟一个空格,树状结构
application.properties示例
server.port=8090
server.session-timeout=30
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8

		application.yml示例
			server:  
  				port: 8090  
  				session-timeout: 30  
  				tomcat.max-threads: 0  
  				tomcat.uri-encoding: UTF-8 

https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#common-application-properties 这个链接中列举了Springboot2.2.2.RELEASE版本的所有配置项,如果需要查找某些配置项,可以从这里拷贝即可;

2. 读取单个配置项

(1)默认配置文件application.properties中的配置项读取:
在application.properties中添加一个配置项:person.hobby=reading ,那么直接使用 @Value("${person.hobby}") 就可以读取到配置项的值
(2)非application.properties中的配置项读取
在resources目录下,创建一个proterties目录,目录下创建一个配置文件test.properties,配置文件内容:test.person.name=tom ,想要使用@Value注解读取这个配置值的,需要指出配置项的类路径:@PropertySource(“classpath:properties/test.properties”)

package com.example.lchtest.springbootdemo1.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
// 不是application.properties中的配置项,要使用@Value注解读取配置值的时候,需要指出配置项的类路径
@PropertySource("classpath:properties/test.properties")
public class SampleController {

    /**
     * 写在application.properties中的配置项,直接使用@value注解获取值
     */
    @Value("${person.hobby}")
    private String hobby;

    @Value("${test.person.name}")
    private String name;

    /**
     * 测试自定义配置文件读取
     * @return
     */
    @RequestMapping("/testproperties")
    @ResponseBody
    public Object test(){
        System.out.println("resources/application.properties 配置项值:" + hobby);
        System.out.println("resources/properties/test.properties 配置项值:" + name);
        return "hobby:" + hobby + "; name:" + name;
    }
}

测试结果:
在这里插入图片描述

3.通过javaBean批量读取自定义配置文件中的配置项

首先,创建一个配置文件:person.proterties
在这里插入图片描述
内容如下:

#自定义配置文件的读取
test.person.name=jack
test.person.age=20
test.person.phone=13613623366

然后创建一个Person的POJO类,用来读取这些配置项,需要注意的是,pojo的属性名,必须是配置文件中最后一个.后面的名称,配置类的头上需要加上三个注解:
@Component // 声明这是spring的一个组件
@PropertySource(“classpath:properties/person.properties”) // 要读取的配置文件的位置
@ConfigurationProperties(prefix = “test.person”) // 配置项的前缀

package com.example.lchtest.springbootdemo1.domain;

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

/**
 * 配置读取类
 * 要求:1. 属性名要和配置项中的名称一致 2.要有getter setter
 */
@Component  // 声明这是spring的一个组件
@PropertySource("classpath:properties/person.properties")  // 要读取的配置文件的位置
@ConfigurationProperties(prefix = "test.person")   // 配置项的前缀
public class Person {
    private String name;
    private int age;
    private  String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

那么问题来了,如何使用这个配置项呢:只需要在使用的地方,通过autowired注解把Person这个类注入进去就可以了:

package com.example.lchtest.springbootdemo1.controller;

import com.example.lchtest.springbootdemo1.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SampleController {

    @Autowired
    private Person person;

    @RequestMapping("/testproperties2")
    @ResponseBody
    public Object testPersonPerties(){
        return person;
    }
}

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值