SpringBoot 中常用的读取配置文件参数的方法

常用的读取配置文件参数的方法有:

1.使用 @Value 注解读取配置
2.使用 @ConfigurationProperties 注解读取配置
3.使用 Environment 对象读取配置
4.使用 PropertiesLoaderUtils 工具读取配置

代码实例:

application.yml

user:
  id: 1
  name: zhangsan
  age: 18

Demo.java

package com.sai.demo.entity;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@Component
public class Demo {
    @Value("${user.id}")
    private String id;
    @Value("${user.name}")
    private String name;
    @Value("${user.age}")
    private String age;
}

Demo2.java

package com.sai.demo.entity;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "user")
public class Demo2 {
    private String id;
    private String name;
    private String age;
}

DemoController

package com.sai.demo.controller;

import com.sai.demo.entity.Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Properties;

/**
 * @param
 * @param
 * @param
 * @author lfc
 * @return
 * @date 2023/4/26
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class DemoController {
    @Autowired
    private Demo demo;
    @Autowired
    private Demo demo2;
    @Autowired
    private Environment environment;

    /**
     * @Value形式
     * @return
     */
    @GetMapping("/getUser")
    public String getUser(){
        System.out.println(demo.toString());
        return demo.toString();
    }

    /**
     * @ConfigurationProperties形式
     * @return
     */
    @GetMapping("/getUser2")
    public String getUser2(){
        System.out.println(demo2.toString());
        return demo2.toString();
    }

    /**
     * 使用 Environment 读取配置形式
     * @return
     */
    @GetMapping("/getUser3")
    public String getUser3(){
        String id = environment.getProperty("user.id");
        String name = environment.getProperty("user.name");
        String age = environment.getProperty("user.age");
        System.out.println("Demo: (id: " + id + ",name: " + name + ",age: " + age + ")");
        return "Demo: (id: " + id + ",name: " + name + ",age: " + age + ")";
    }

    /**
     * 使用 PropertiesLoaderUtils 读取配置形式
     * @return
     */
    @GetMapping("/getUser4")
    public String getUser4(){
        String id = null;
        String name = null;
        String age = null;
        try {
            ClassPathResource resource = new ClassPathResource("application.yml");
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            id = properties.getProperty("id");
            name = properties.getProperty("name");
            age = properties.getProperty("age", "100");

        } catch (IOException e) {
            log.error("", e);
        }
        System.out.println("Demo: (id: " + id + ",name: " + name + ",age: " + age + ")");
        return "Demo: (id: " + id + ",name: " + name + ",age: " + age + ")";
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值