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 { }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)