springboot配置之获取配置文件中属性的第二种方法(@Value)不同于@ConfigurationProperties

直接看:Person.java

package com.gong.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

//将配置文件中的属性映射到组件中
//prefix:表示配置文件中的哪个下面的属性进行一一映射
@Component
//@ConfigurationProperties(prefix="person")
public class Person {
    /**<bean clas="Person">
     *      <property name="username" value="字面量/${key}从环境变量中获取值/#{}spel"></property>
     * </bean>
     *
     */
    @Value("${person.username}")
    private String username;
    @Value("#{11*2}")
    private Integer age;
    @Value("[email protected]")
    private String email;    //@Value{"person.maps"} 会报错
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;    ......]

运行测试:

Person{username=‘张三‘, age=22, email=‘[email protected]‘, maps=null, lists=null, dog=null}

它们之间的不同点:

  • ConfigurationProperties:批量注入配置文件中的属性,Value:一个个绑定
  • ConfigurationProperties:支持松散绑定。所谓松散绑定,就是在配置文件中使用:
    last-name、last_name、lastName都会被标识为lastName。而Value中不支持。
  • ConfigurationProperties不支持spel表达式,Value中支持。
  • ConfigurationProperties支持JSR303校验,Value不支持。
  • ConfigurationProperties支持复杂类型封装,Value不支持,也就是说Value不支持Map等。

原文地址:https://www.cnblogs.com/xiximayou/p/12246489.html

时间: 2024-07-30 09:12:29

springboot配置之获取配置文件中属性的第二种方法(@Value)不同于@ConfigurationProperties的相关文章

springboot配置之在配置文件中配置debug=true开启自动配置类报告

=========================== CONDITIONS EVALUATION REPORT ============================ Positive matches: ----------------- AopAutoConfiguration matched: - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition) AopAutoConfiguration

获取对象中值的两种方法

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="button" name="" id="btn" value="按钮" /> <input type=&

获取Unity3D中物体的几种方法

public或者[serializabled]修饰变量 GameObject.FindWithTag 通过标签 Object.FindObjectOfType 通过挂载的组件 GameObject.Find (全局或者通过"\"进行层级查找) Transform.Find 查找子物体 非递归查找子物体,GetComponentsInChildren<Transform>()

Knockout获取数组元素索引的2种方法,在MVC中实现

在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespace UseIndex.Models { public class Student { public int Id { get; set; } public string Name { get; set; } } } 在HomeController中,先模拟一个Student的集合,在投影出Name属性的集合,最后以Json返回给前台视图. using Syste

js+jquery动态设置/增加/删除/获取元素属性的两种方法集锦对比(动态onclick属性设置+动态title设置)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>

QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)

QT中获取选中的radioButton的两种方法 QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast<QRadioButton*>(ui->BG->checkedButton()); 2 QString name = pbtn->objectName(); 3 if(!QString::compare(name, "rad

Android中WebView获取网页中标题 ,内容, 图片的方法

如题,在Android中WebView获取网页中标题 ,内容, 图片的方法 首先是获取标题,在new WebChromeClient(){}中重写onReceivedTitle()方法 @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); // loge.e("__页面标题__"+title); } 获取内容,是参考的这边的 http

PHP中获取文件扩展名的N种方法

PHP中获取文件扩展名的N种方法 从网上收罗的,基本上就以下这几种方式: 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($file) { return substr($file, strrpos($file, '.')+1); } 第3种方法: function get_extension($file) { return end(expl

类别中使用属性的两种方法

类别因不能添加实例变量,所以添加属性不能生成实例变量也不能自动生成setter跟getter方法,但可以自己实现setter跟getter方法. 第一种使用属性的方法:自己实现setter跟getter方法,用参数的值判断对主类中实例变量做一些操作. .h文件代码: #import "Fimaly.h" @interface Fimaly (Play) @property (nonatomic,assign) int age; @end .m文件代码 #import "Fim