`
buddie
  • 浏览: 183007 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SpringBoot自定义YAML配置类

 
阅读更多

在开发SpringBoot应用程序中,可以使用yaml文件来配置各种属性及参数,并可以直接映射到Java类的属性当中。

比如,我有一个Java类 UserProperties.java

package cn.buddie.test.yaml;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 用户配置
 */
public class UserProperties {
    /**
     * 名称
     */
    private String userName;
    /**
     * 性别
     */
    private int gender;

    // getter & setter
    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getUserName() {
        return userName;
    }

    public int getGender() {
        return gender;
    }
}

 

 我期望能在yaml文件中配置UserProperties中的属性,当我需要使用时,直接从UserProperties类中取就可以了。

 

只需要给UserProperties类中加入两个注解就可以了

 

@ConfigurationProperties("user")
@Component
public class UserProperties

 

 

其中@ConfigurationProperties表示这是一个注解类,"user":表示要解析yaml文件中user开头的配置

@Component表示要将此类做作一个组件,注册到Spring容器中,方便我们的后面通过自动注入来使用。

然后yaml配置文件中,对UserProperties中的属性通过配置就可以

application.yaml

user:
  user-name: 'zhangsan'
  gender: 2

在java类中属性userName,在yaml中,也可以写成‘user-name’,规则就是把大写字母为成'-'+对应的小写字母 

 

写一个测试类,测试一下

 

 

package cn.buddie.test.yaml;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(SpringRunner.class)
@SpringBootTest
public class YamlTest {

    @Autowired
    private UserProperties userProperties;

    @Test
    public void testYaml() {
        System.out.println(userProperties.getUserName() + "-" + userProperties.getGender());
    }
}

 

测试结果

zhangsan-2

 

需要注意的地方

1、需要依赖jar包:

compileOnly("org.springframework.boot:spring-boot-configuration-processor")

2、属性不能直接用'name',这样会取到系统环境中的'name'

3、被@ConfigurationProperties注解的配置类,不能为内部类

 

 

分享到:
评论

相关推荐

    SpringBoot学习视频

    尚硅谷_SpringBoot_配置-yaml配置文件值获取 尚硅谷_SpringBoot_配置-properties配置文件编码问题 尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 尚硅谷_SpringBoot_配置-@PropertySource、@...

    尚硅谷springboot视频(上)

    尚硅谷_SpringBoot_配置-yaml配置文件值获取 尚硅谷_SpringBoot_配置-properties配置文件编码问题 尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 尚硅谷_SpringBoot_配置-@PropertySource、@...

    128元尚硅谷Java视频教程_SpringBoot视频教程(上)核心技术篇

    11、尚硅谷_SpringBoot_配置-yaml配置文件值获取 12、尚硅谷_SpringBoot_配置-properties配置文件编码问题 13、尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 14、尚硅谷_SpringBoot_配置-@...

    尚硅谷Spring boot核心技术篇(上)

    09、尚硅谷_SpringBoot_配置-yaml简介 10、尚硅谷_SpringBoot_配置-yaml语法 11、尚硅谷_SpringBoot_配置-yaml配置文件值获取 12、尚硅谷_SpringBoot_配置-properties配置文件编码问题 13、尚硅谷_SpringBoot_配置-@...

    Java SpringBoot课件+源码视频教程

    11、_SpringBoot_配置-yaml配置文件值获取 , y( ] x3 r% o7 ]5 i 12、_SpringBoot_配置-properties配置文件编码问题 13、_SpringBoot_配置-@ConfigurationProperties与@Value区别 14、_SpringBoot_配置-@Property...

    Spring Boot中文文档.rar

    禁用特定的自动配置类 17. Spring Beans和依赖注入 18.使用@SpringBootApplication Annotation 19.运行您的应用程序 19.1.从IDE运行 19.2.作为打包应用程序运行 19.3.使用Maven插件 19.4.使用...

    springboot参考指南

    使用YAML配置外部属性 v. 63.5. 设置生效的Spring profiles vi. 63.6. 根据环境改变配置 vii. 63.7. 发现外部属性的内置选项 iii. 64. 内嵌的servlet容器 i. 64.1. 为应用添加Servlet,Filter或...

    基于Java和Vue的免费表单问卷系统设计源码

    免费表单问卷系统:基于Java和Vue开发,包含315个文件,包括275个Java类文件、13个PNG图像文件、9个XML配置文件、4个HTML文件、3个Markdown文档、3个SQL文件、2个YAML配置文件、1个.gitignore文件和1个LICENSE文件。...

    基于Dubbo实现的SOA分布式(没有实现分布式事务)-SpringBoot整合各种组件的JavaWeb脚手架+源代码+文档

    - 在Controller中使用@PreAuthorize等注解需要在spring-web配置文件中扫描security包下的类 6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + ...

    Spring高级之注解驱动开发视频教程

    n 高级特性-自定义PropertySourceFactory实现解析yaml配置文件 n 源码分析-BeanFactory类视图和常用工厂说明 n 源码分析-AnnotationConfigApplicationContext的register方法 n 源码分析-...

Global site tag (gtag.js) - Google Analytics