读取yml配置文件中的值

1.yml文件

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
      - lisi
      - zhaoliu
    dog:
      name: 小狗
      age: 12

2.javaBean

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *      prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 *
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;}

3.pom.xml导入依赖

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

注:这里可能需要启动一下项目,使之生效

[email protected] 注解获取yml配置文件中的数据,以及@Validated进行数据校验

@Component
//@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式
    @Email
    @Value("${person.last-name}")
    private String lastName;
    @Value("#{11*2}")
    private Integer age;
    @Value("true")
    private Boolean boss;

    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

[email protected](prefix = "person") 和 @value的区别

|            | @ConfigurationProperties | @Value |
| ---------- | ------------------------ | ------ |
| 功能               | 批量注入配置文件中的属性             | 一个个指定  |
| 松散绑定(松散语法)   | 支持                                | 不支持    |
| SpEL               | 不支持                            | 支持     |
| JSR303数据校验       | 支持                              | 不支持    |
| 复杂类型封装          | 支持                               | 不支持    |

6.总结

配置文件yml还是properties他们都能获取到值;

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

原文地址:https://www.cnblogs.com/gxlaqj/p/11676875.html

时间: 2024-07-31 14:03:58

读取yml配置文件中的值的相关文章

SpringBoot 中从yml配置文件中读取常用的参数值

SpringBoot现在基本上都是使用application-XXX.yml(生产环境为pro,开发测试环境为dev)来配置项目中的一些配置条件,在springboot中还可以通过从yml文件中将yml中的数据直接读取出来. 1.yml文件(这里设置参数的时候往往设置两层前缀,避免在调用的时候该属性中的部分参数名与计算机中的某些名字冲突) test: person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1:

springboot属性类自动加载配置文件中的值

springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,prefix为yml中配置的属性名称,要想属性类生效得加上@Component注解 如果想要在yml中有对应类的提示,还需要添加如下依赖: yml书写如下: 如果是properties文件,则书写如下: 在yml中如果值中有特殊字符,需要转义可以用单引号包裹,默认是双引号 如果仅仅为类中的某个属性值赋

Eclipse RCP 中创建自定义首选项,并能读取首选项中的值

Eclipse RCP的插件中若想自己定义首选项需要扩展扩展点: org.eclipse.core.runtime.preferences //该扩展点用于初始化首选项中的值 org.eclipse.ui.preferencePages//该扩展点用于定义自己的首选项页面 plugin.xml中内容如: Database Preferences挂在WorkFlowBase下,需要在category中填写workFlowBase的ID WorkFlowPreferenceInitializer类,

Spring对象实例化,获取配置文件中的值(property为IDictionary类型的数据)

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; namespace WeiXin.Core { public static class JobBase { private static IDictionary<string, string> ParNameDictionary; private static IDictionary<

通过key键读取到配置文件中对应的值的接口实现

/*********************************************************************  * Author  : Samson  * Date    : 03/13/2015  * Test platform:  *              3.13.0-24-generic  *              GNU bash, 4.3.11(1)-release  * ************************************

读取配置文件中的值

/** * 加载配置文件 * @author * */public class ConfigUtil { //实例属性(对象属性) private static Properties ps = new Properties(); static{ /* * 在加载类的同时,告诉类加载器,将此文件加载到内存 */ InputStream in = ConfigUtil.class.getClassLoader().getResourceAsStream("db.properties");

SpringBoot通过@Value获取application.yml配置文件的属性值

application.yml实例: spring: redis: database: 0 host: 127.0.0.1 获取方法: /** * @Auther:WangZiBin * @Description: * @Modified By: */ @Configuration public class JedisConfig{ private Logger jedisConfigLogger= LoggerFactory.getLogger(JedisConfig.class); @Val

springboot----&gt;获取不到yml配置文件指定的值

1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 @SpringBootApplication 4 public class App { 5 public static void main(String[] args) { 6 SpringApplication app = new SpringA

使用CString类型的变量读取ini配置文件中的数据后,使用ReleaseBuffer释放

如果未使用 ReleaseBuffer,会导致Find()函数查找失败! 1 { 2 GetPrivateProfileString("SZDeskConnections", strTemp.Right(1),NULL,strBuffer.GetBuffer(100),100,".\\RQTrans.INI"); 3 strBuffer.ReleaseBuffer(); 4 } 5 int iPos[8] = {0}, nIndex(0); 6 while (nIn