SpringBoot yml属性自动注入
2018-03-31 22:43
477 查看
SpringBoot yml属性自动注入
本文讲的是如何把一个yml文件中自定义的属性集合映射到一个类上1.自定义yml 内容
// 以微信的配置为例 weixin: appid: xxxxxxxxx appSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx apiSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx grantType: authorization_code
2.向spring注册组件类
@Slf4j @Data @NoArgsConstructor @AllArgsConstructor @Component @ConfigurationProperties(prefix = "weixin") public class WeixinProperties { private String appid, appSecret, apiSecret, grantType; }
3.添加注册依赖
// 如果不添加注册依赖,@ConfigurationProperties IDEA会报错,找不到属性 // 依赖添加好之后,maven reimport更新依赖 // 鼠标点击组件的字段,按住Ctrl可以导航到yml中定义的字段中 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
4.注入组件
@Data @NoArgsConstructor @AllArgsConstructor @Configuration @SuppressWarnings(IDEWarnType.ALL) public class WeixinConfig { // 自动注入组件 @Autowired private WeixinProperties weixin; 4000 }
相关文章推荐
- Spring boot 学习笔记(1)——hello world创建,属性application.properties、 yml配置
- 笔记--Spring Boot中解析yml配置 注入到对象
- spring boot 中@Autowired注解无法自动注入的错误
- spring在filter和servlet中无法自动注入属性
- Spring Boot @Autowired 没法自动注入的问题
- Spring依赖注入 — 属性自动装配
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- SSH整合问题:struts2-spring-plugin自动注入action的属性所产生的问题
- struts和spring的plugin产生Action的时候会自动把action的属性注入进来,默认按名字
- spring boot 注入properties属性文件的值 @ConfigurationProperties
- spring对抽象类中属性的自动注入
- SpringBoot属性文件配置(application.properties/yml)
- Spring Boot @Autowired 没法自动注入的问题
- 关于spring boot无法自动注入bean问题解决方案
- spring-自动加载配置文件\使用属性文件注入
- Spring依赖注入 — 属性自动装配
- Spring boot将配置属性注入到bean类中
- spring 常用属性注入和自动装配