springboot中自定义配置

简介: springboot中自定义配置

我们不快乐的原因之一,是不知道如何安静地待在房间里,心平气和地与自己相处。——亦舒

例如我们需要进行一些自定义配置写到配置文件中

可以使用@ConfigurationProperties注解

package com.ruben.pojo;
import com.ruben.enumeration.GenderEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
 * @ClassName: RubenProperties
 * @Description: ruben配置类
 * @Date: 2021/2/16 0016 11:40
 * *
 * @author: <achao1441470436@gmail.com>
 * @version: 1.0
 * @since: JDK 1.8
 */
@Data
@Component
@ConfigurationProperties(prefix = "ruben")
public class RubenProperties {
    private Integer number;
    private String avatar;
    private GenderEnum gender;
    private List<String> hobby;
    private Map<String, Object> introduce;
}

这里枚举GenderEnum

@Getter
@AllArgsConstructor
public enum GenderEnum {
    FEMALE("女", 0),
    MALE("男", 1);
    private final String name;
    private final Integer code;
}

然后yml中对应配置写法

ruben:
  number: 4444
  avatar: /imgs/oss/2020-06-01/head.jpg
  gender: male
  hobby: ["游戏","动漫","编程"]
  introduce: {"food": "blood","programLanguage": "java"}

我们编写一个测试类测试

package com.ruben;
import com.ruben.pojo.RubenProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Map;
/**
 * @ClassName: PropertiesDemo
 * @Description: 我还没有写描述
 * @Date: 2021/2/16 0016 11:29
 * *
 * @author: <achao1441470436@gmail.com>
 * @version: 1.0
 * @since: JDK 1.8
 */
@Slf4j
@SpringBootTest
public class PropertiesDemo {
    @Resource
    private RubenProperties rubenProperties;
    @Test
    public void test() {
        log.info(rubenProperties.toString());
    }
}

运行结果

相关文章
|
1天前
|
Java 数据库连接 Spring
面试题:springboot的自定义配置有哪些
面试题:springboot的自定义配置有哪些
21 0
|
1天前
|
Java
springboot WebMvcConfigurer详解自定义配置请求静态资源
springboot WebMvcConfigurer详解自定义配置请求静态资源
|
1天前
|
Java 数据库连接 Maven
SpringBoot【付诸实践 01】SpringBoot自定义starter保姆级教程(说明+源码+配置+测试)
SpringBoot【付诸实践 01】SpringBoot自定义starter保姆级教程(说明+源码+配置+测试)
38 1
|
10月前
|
SQL 关系型数据库 MySQL
SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入
SpringBoot自定义配置注入的方式:自定义配置文件注入,从mysql读取配置进行注入
166 0
|
8月前
|
Java 容器
SpringBoot中的yml文件中读取自定义配置信息
SpringBoot中的yml文件中读取自定义配置信息
92 0
|
10月前
|
安全 Java Spring
SpringBoot整合Spring Security,自定义登录成功/失败处理器,配置登录人数(三)
一般采用的是实现接口的方式:implements AuthenticationSuccessHandler 但是如果想要实现登录成功后跳转回登录前的页面可以直接继承SavedRequestAwareAuthenticationSuccessHandler这个类,该类的父类SimpleUrlAuthenticationSuccessHandler实现了AuthenticationSuccessHandler。
219 0
|
前端开发 Java Nacos
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
559 1
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
|
Dubbo Java 应用服务中间件
SpringBoot整合Dubbo的第二种方式——API(自定义Configuration配置类)
SpringBoot整合Dubbo的第二种方式——API(自定义Configuration配置类)
SpringBoot整合Dubbo的第二种方式——API(自定义Configuration配置类)
|
NoSQL Java Redis
【SpringBoot 基础系列】实现一个自定义配置加载器(应用篇)
Spring 中提供了@Value注解,用来绑定配置,可以实现从配置文件中,读取对应的配置并赋值给成员变量;某些时候,我们的配置可能并不是在配置文件中,如存在 db/redis/其他文件/第三方配置服务,本文将手把手教你实现一个自定义的配置加载器,并支持@Value的使用姿势
452 0
|
Java Spring 容器
Spring Boot加载自定义配置方案
Spring Boot加载自定义配置方案
186 0