springboot-读取应用配置

1、Environment

一个通用的读取应用程序运行时的环境变量的类,可以读取application.properties,命令行输入参数,系统属性,操作系统环境变量等。可以通过Spring容器自动注入。Environment可以用在Spring应用的任何地方。

EnvConfig

复制代码
package com.gcz.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author guocz
 * @date 20210713
 * 环境参数
 */
@Configuration
public class EnvConfig {

    @Autowired
    private Environment environment;

    public String getServerPort() {
        return environment.getProperty("server.port", String.class);
    }

    public String getUserDir(){
        return environment.getProperty("user.dir", String.class);
    }

    public String getUserHome(){
        return environment.getProperty("user.home", String.class);
    }

    public String getJavaHome(){
        return environment.getProperty("JAVA_HOME", String.class);
    }
}
复制代码

controller

复制代码
package com.gcz.controller;

import cn.hutool.core.util.StrUtil;
import com.gcz.config.EnvConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author guocz
 * @date 20210713
 * 读取应用配置
 */
@Controller
@RequestMapping("/prop")
public class PropertiesController {

    @Autowired
    private EnvConfig envConfig;

    /**
     * 服务端口
     */
    private final static String SERVER_PORT = "server.port";

    /**
     * 程序运行目录
     */
    private final static String USER_DIR = "user.dir";

    /**
     * 用户home目录
     */
    private final static String USER_HOME = "user.home";

    /**
     * 读取环境变量
     */
    private final static String JAVA_HOME = "JAVA_HOME";

    @RequestMapping(value = "/{param}")
    @ResponseBody
    public String getProperties(@PathVariable String param){
        if (StrUtil.equals(param, SERVER_PORT)) {
            return envConfig.getServerPort();
        }else if (StrUtil.equals(param, USER_DIR)) {
            return envConfig.getUserDir();
        }else if (StrUtil.equals(param, USER_HOME)) {
            return envConfig.getUserHome();
        }else if (StrUtil.equals(param, JAVA_HOME)) {
            return envConfig.getJavaHome();
        }else {
            return "not exist";
        }
    }
}
复制代码

 2、@Value

直接通过@Value注解注入一个配置信息到Spring管理的Bean中

controller

复制代码
   /**
     * 读取配置
     * @param port
     * @return
     */
    @RequestMapping("/getValue")
    @ResponseBody
    public String getValue(@Value("${server.port}") String port) {
        return port;
    }
复制代码

or

复制代码
   /**
     * 端口号
     */
    @Value("${server.port}")
    private String value;
   /**
     * 读取配置
     * @return
     */
    @RequestMapping("/getValue1")
    @ResponseBody
    public String getValue() {
        return value;
    }
复制代码

 3、@PropertySource

如果将所有的配置都集中到 application.properties 或 application.yml 中,那么这个配置文件会十分的臃肿且难以维护,因此我们通常会将与 Spring Boot 无关的配置(例如自定义配置)提取出来,写在一个单独的配置文件中,并在对应的 JavaBean 上使用 @PropertySource 注解指向该配置文件。

@PropertySource(value = "classpath:person.properties")//指向对应的配置文件
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    
}

4、@ConfigurationProperties

通过 Spring Boot 提供的 @ConfigurationProperties 注解,可以将全局配置文件中的配置数据绑定到 JavaBean 中。

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    
}

 

posted @   幻影黑子  阅读(168)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示