SpringBoot(四):java从配置文件中取值的方式
一、SpringBoot项目中取yaml配置文件中的值
application.yaml
test:
url: localhost:8080
name: root
password: 123456
val:
a: 1
b: 2
c: 3
TestConfig.class
@Component @ConfigurationProperties(prefix = "test") public class TestConfig { private String url; private String name; private String password; private Map<String, Object> val; public TestConfig() { } public TestConfig(String url, String name, String password, Map<String, Object> val) { this.url = url; this.name = name; this.password = password; this.val = val; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Map<String, Object> getVal() { return val; } public void setVal(Map<String, Object> val) { this.val = val; } @Override public String toString() { return "TestConfig{" + "url='" + url + '\'' + ", name='" + name + '\'' + ", password='" + password + '\'' + ", val=" + val + '}'; } }
测试类:
@SpringBootTest class Springboot02ConfigApplicationTests { private TestConfig testConfig; @Autowired public void setTestConfig(TestConfig testConfig) { this.testConfig = testConfig; } @Test void contextLoads() { System.out.println(testConfig); } }
测试结果:
在SpringBoot项目中取得yaml配置文件的内容,核心就是@ConfigurationProperties注解。
二、取properties配置文件中的值
jms.properties
url=localhost:8081
name=root1
password=1234567
TestConfig.class
@Component @PropertySource("classpath:jms.properties") public class TestConfig { @Value("${url}") private String url; @Value("${name}") private String name; @Value("${password}") private String password; private Map<String, Object> val; public TestConfig() { } public TestConfig(String url, String name, String password, Map<String, Object> val) { this.url = url; this.name = name; this.password = password; this.val = val; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Map<String, Object> getVal() { return val; } public void setVal(Map<String, Object> val) { this.val = val; } @Override public String toString() { return "TestConfig{" + "url='" + url + '\'' + ", name='" + name + '\'' + ", password='" + password + '\'' + ", val=" + val + '}'; } }
测试结果:
核心注解@Value,这里用到@propertySource是为了不不使用默认的application配置文件,若为application.properties,可不使用此注解。
三、普通项目中得到properties配置文件中的值
jdbc.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/blms?serverTimezone=UTC
name=root
password=123456
测试类
public class test { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); String driver = bundle.getString("driver"); String url = bundle.getString("url"); String name = bundle.getString("name"); String password = bundle.getString("password"); System.out.println(driver + "\n" + url + "\n" + name + "\n" + password); } }
(本文仅作个人学习用,如有纰漏敬请指正)
分类:
SpringBoot学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!