1. Maven 的pom.xml 引入springboot
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>
2. 编写application.yml
在类路径中,如resource目录下建立application.yml
mqtt: url: tcp://127.0.0.1:1883 user: yourUsername passwd: yourPasswd clientId: yourClientId defaultTopic: default/topic
3. 代码中
YamlPropertiesFactoryBeanfactoryBean=newYamlPropertiesFactoryBean(); factoryBean.setResources(newClassPathResource("application.yml")); Propertiesenv=factoryBean.getObject(); Stringurl=env.getProperty("mqtt.url"); Stringuser=env.getProperty("mqtt.user"); Stringpasswd=env.getProperty("mqtt.passwd"); Stringtopic=env.getProperty("mqtt.defaultTopic");