• 读取的配置文件application.yml:

    jianshu:id: futaosmile

1. 使用@ConfigurationProperties注解注入

@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "jianshu")
public class SpringmvcdemoApplicationTests {private String id;@Testpublic void test10() {System.out.println(id);}
}
  • 输出null,注入失败
  • 原因:使用@ConfigurationProperties注解的方式注入为每个注入的字段添加setter方法
package com.futao.springmvcdemo;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
@ConfigurationProperties(prefix = "jianshu")
public class SpringmvcdemoApplicationTests {private String id;@Testpublic void test10() {System.out.println(id);}public void setId(String id) {this.id = id;}
}
  • 输出:

2. 使用@Value注解

package com.futao.springmvcdemo;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringmvcdemoApplicationTests {@Value("${jianshu.id}")private String id;@Testpublic void test10() {System.out.println(id);}}
  • 输出:

OK

@ConfigurationProperties注解的使用与@Value的使用相关推荐

  1. Spring Boot 关于 @EnableConfigurationProperties 注解 —— 使用 @ConfigurationProperties 注解的类生效。

    先说作用: @EnableConfigurationProperties注解的作用是:使用 @ConfigurationProperties 注解的类生效. 说明: 如果一个配置类只配置@Config ...

  2. 在Spring Boot中使用 @ConfigurationProperties 注解

    @ConfigurationProperties根据类型校验和管理application中的bean. Spring Boot 使用一些松的规则来绑定属性到@ConfigurationProperti ...

  3. @Value@PropertySource@ConfigurationProperties注解使用

    使用配置文件注入属性 Spring Boot 默认的配置文件src/main/java/resources/application.properties或者src/main/java/resource ...

  4. @EnableConfigurationProperties 注解和@ConfigurationProperties注解实现配置绑定

    ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@Config ...

  5. @Value与@ConfigurationProperties注解的讲解与区别

    @Value和@ConfigurationProperties: @Value注解与@ConfigurationProperties都是对对象属性进行注入配置的注解. @Value注解: //源码: ...

  6. SpringBoot:@ConfigurationProperties注解使用与源码

    文章目录 1.美图 2.概述 2.1 源码 3.猜想 4.@SpringBootApplication 5.@EnableAutoConfiguration 1.美图 2.概述 我们在使用Spring ...

  7. @ConfigurationProperties注解使用

    1.基本简介 @ConfigurationProperties注解用于自动配置绑定,可以将application.properties配置中的值注入到bean对象上. 该注解使用必须将对象注入到IOC ...

  8. SpringBoot——@ConfigurationProperties注解

    @ConfigurationProperties注解(将配置文件中的配置,以属性的形式自动注入到实体中)可以注入在application.properties配置文件中的属性,和@Bean 或者 @C ...

  9. 【SpringBoot】SpringBoot @ConfigurationProperties 注解 用法与加载static静态属性

    @ConfigurationProperties [SpringBoot]SpringBoot @ConfigurationProperties 注解 用法与加载static静态属性 @Configu ...

最新文章

  1. Ubuntu 16.04安装双显卡驱动方法收集
  2. SQL语句学习之路3
  3. sqlserver order by自定义数字排序 其他_苹果cms怎么自定义伪静态规则?
  4. python学多久能写东西的软件有哪些_怎么自学python,大概要多久?
  5. 【C++探索之旅】第二部分第一课:面向对象初探,string的惊天内幕
  6. 虚拟化+云服务器,虚拟化+云服务器
  7. java中多线程重要吗_Java基础知识_多线程必要知识点
  8. 相机模型与标定(十)--RANSAC算法
  9. 【Django 2021年最新版教程19】数据库查询 model filter 条件或or
  10. ci mysql空闲连接回收_数据库连接空闲回收问题 CommunicationsException: Communications link failure...
  11. C语言 设计项目课题,C语言课程设计课题.doc
  12. 「硬见小百科」很全的二极管干货
  13. 计算机网络和internet选项,详细教你电脑ie的internet选项在哪
  14. DART booster
  15. C++排雷:19.过滤英文和中文标点符号,string与wstring之间的转换
  16. adb shell getprop/setprop
  17. 清华大学计算机学院曹伦郗,四川文理第一名填报北大 高分“学霸”们最后选了啥专业...
  18. php将阿拉伯数字转换成中文大写,PHP将阿拉伯数字转换成汉字大写支持小数点
  19. 医疗保健数据接口_为什么需要用于医疗保健业务的应用程序
  20. [thread 38768 also had an error]

热门文章

  1. linux nfs建立共享目录,linux下nfs磁盘共享目录
  2. vb excel编程实例_用过程和函数来解决VB中的计算问题
  3. python 地理信息_GitHub - sujeek/geospatial-data-analysis-cn: Python地理信息数据教程中文版(GeoPandas、GIS)...
  4. eclipse的jsp第一行代码报错_机器学习之AdaBoost算法及纯python代码手工实现
  5. stm32捕获占空比_基于STM32超声波避障小车
  6. linux ubuntu 17,Ubuntu 17.04(Zesty Zapus)已经结束支持,请升级到Ubuntu 17.10
  7. 客户端访问https时应无浏览器(含终端)安全警告信息;_https和http有什么区别(内附详细分析)...
  8. 邮件header中的subject或者from解码
  9. mysql移植海思_minigui在海思解决方案(hi3515芯片)上的移植过程(转)
  10. 笔记-高项案例题-2016年下-项目收尾