SpringBoot引入配置文件属性自定义为Bean
- 在配置文件中写入配置属性
wll.test.name=vsalw wll.test.age=888 |
- 自定义一个实体类
//@Component @Data @ConfigurationProperties(prefix = “wll.test”)//前缀 public class Userproperties { private String name; private Integer age; } |
- 自定义配置类来配置实体类
@Configuration @EnableConfigurationProperties(EmqConnectProperties.class) @ConditionalOnProperty(prefix = “emq.connect”, name = { “user”, “password”, “host”, “clientId”, “topic”,”qos”}) public class EmqConnectConfiguration { } |
4.使用的类中这样用
@SpringBootApplication public class DemoApplication { private static Userproperties user; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println(“toString:” + user.toString()); } public DemoApplication(Userproperties user) { this.user = user; } } |
另一种:
把第二步中@Component放开,不需要第3步哪个配置文件,其他不变,照样用。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。