当前位置: 首页 > ai >正文

Java开发小知识-获取配置文件的值(转为Java对象)

创建项目:
在这里插入图片描述
引入依赖:

     <!--spring boot 项目集成消息中间件基础依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration</artifactId><version>3.0.5</version></dependency><!--spring boot 项目和mqtt客户端集成起步依赖--><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-mqtt</artifactId><version>5.4.3</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--fastjson--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency>

配置application.yml文件做需要的属性

spring:mqtt:username: lisipassword: 123456url: 123123subClientId: 456subTopic: hellopubClientId: hello

创建配置属性类
在这里插入图片描述
在配置属性类上加注解
@Data
@ConfigurationProperties(prefix = “spring.mqtt”)

package mqtt;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@Data
@ConfigurationProperties(prefix = "spring.mqtt")
public class MqttProperties {private String username;private String password;private String url;private String subClientId;private String subTopic;private String pubClientId;
}

在启动类上加注解 @EnableConfigurationProperties(value = MqttProperties.class)

package mqtt;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
@EnableConfigurationProperties(value = MqttProperties.class)
public class MqttApplicaiton {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(MqttApplicaiton.class, args);MqttProperties properties = context.getBean(MqttProperties.class);System.out.println("MQTT Username: " + properties.getUsername());System.out.println("MQTT Password: " + properties.getPassword());System.out.println("MQTT URL: "      + properties.getUrl());System.out.println("SubClientId: "   + properties.getSubClientId());}
}

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

http://www.xdnf.cn/news/14514.html

相关文章:

  • AIGC工具平台-VideoRetalking音频对口型数字人
  • 前端如何禁止用户复制?
  • vue3 el-select @change (val) 多参数传值操作
  • HCIP-数据通信基础
  • swift-14-可选项的本质、运算符重载、扩展
  • 【案例】性能优化在持续集成与持续交付中的应用
  • RPGMZ游戏引擎 如何手动控制文字显示速度
  • 传输层协议UDP/TCP
  • 【linux】bash脚本中括号问题
  • 巧用云平台API实现开源模型免费调用的实战教程
  • Linux嵌入式和单片机嵌入式的区别?
  • 数据库从零开始:MySQL 中的 DDL 库操作详解【Linux版】
  • excel 数据透视表介绍
  • 技术革新赋能楼宇自控:物联网云计算推动应用前景深度拓展
  • 【图像处理入门】11. 深度学习初探:从CNN到GAN的视觉智能之旅
  • Arduino入门教程:11、直流步进驱动
  • C#语言入门-task2 :C# 语言的基本语法结构
  • Oracle 中唯一索引对行锁的影响
  • 【支持向量机】SVM线性可分支持向量机学习算法——硬间隔最大化支持向量机及例题详解
  • 股票心理学习篇:交易的人性弱点 - 频繁交易
  • GNSS介绍
  • 基于React+Express的个人账单管理系统
  • 【Linux手册】进程优先级:操作系统世界里的“资源争夺”
  • Redis 的优势有哪些,它是CP 还是 AP?CAP 理论又是什么?
  • SpringBoot扩展——发送邮件!
  • 医疗低功耗智能AI网络搜索优化策略
  • 跨越网络界限,轻松实现广域网打印——HttpPrinter报表打印解决方案
  • Linux基本指令篇 —— mkdir指令
  • 苍穹外卖--WebSocket、来单提醒、客户催单
  • 【漏洞复现】Apache Kafka Connect 任意文件读取漏洞(CVE-2025-27817)