自定义类读取properties/yml配置文件

本文介绍了一种Spring Boot读取配置文件的方法,通过创建特定类来接收YML配置,避免使用@Value。文章展示了如何用Map接收log/mail下的属性,定义并注册UserMqProperties类,以及在不同场景下如何使用@ConfigurationProperties注解进行配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里记录一种spring boot读取配置文件的方式,不讨论@Value的方式了,那个太简单了

假设我们Yml中有配置:

   

##### 自定义队列信息
user:
  mq:
    log:
      queue: logQueue
      exchange: logExchange
      routingKey: logRk
    mail:
      queue: mailQueue
      exchange: mailExchange
      routingKey: mailRk

新建已给类来接收

   

package com.wm.contextinit.config;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Map;

/***
 * @ClassName: UserMqProperties
 * @Description:
 * @Author: wm_yu
 * @Create_time: 14:12 2019-12-18
 */
@ConfigurationProperties("user.mq")
@Data
@ToString
public class UserMqProperties {

    private Map<String,String> log;
    private Map<String,String> mail;

    @Bean("logMqProperties")
    public MqProperties logMqProperties(){
        return new MqProperties(log.get("queue"),log.get("exchange"),log.get("routingKey"));
    }

    @Bean("mailMqProperties")
    public MqProperties mailMqProperties(){
        return new MqProperties(mail.get("queue"),mail.get("exchange"),mail.get("routingKey"));
    }

}

上图是用map接收log/mail下的属性

在定义一个类

package com.wm.contextinit.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

/***
 * @ClassName: MqProperties
 * @Description:
 * @Author: wm_yu
 * @Create_time: 14:25 2019-12-18
 */
@Data
@AllArgsConstructor
@ToString
public class MqProperties {
    private String queue;
    private String exchange;
    private String routingKey;
}

在使用的地方使用注解:

@EnableConfigurationProperties({UserMqProperties.class})

如下:

package com.wm.contextinit.service;

import com.wm.contextinit.config.MqProperties;
import com.wm.contextinit.config.UserMqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/***
 * @ClassName: TestService
 * @Description:
 * @Author: wm_yu
 * @Create_time: 14:19 2019-12-18
 */
@Service
@Slf4j
@EnableConfigurationProperties({UserMqProperties.class})
public class TestService {

    @Autowired
    private UserMqProperties userMqProperties;

    @Autowired
    private MqProperties logMqProperties;


    @Autowired
    private MqProperties mailMqProperties;

    public void test(){
        log.info("userMqProperties:[{}],logMqProperties:[{}],mailMqProperties:[{}]",userMqProperties.toString(),logMqProperties.toString(),mailMqProperties.toString());
    }



}

测试如下:

 

其实还可以更简单,如下:

直接在UserMqProperties上添加注解@Compment 注册到ioc

package com.wm.contextinit.config;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;

/***
 * @ClassName: UserMqProperties
 * @Description:
 * @Author: wm_yu
 * @Create_time: 14:12 2019-12-18
 */
@ConfigurationProperties("user.mq")
@Data
@ToString
@Component
public class UserMqProperties {

    private Map<String,String> log;
    private Map<String,String> mail;

    @Bean("logMqProperties")
    public MqProperties logMqProperties(){
        return new MqProperties(log.get("queue"),log.get("exchange"),log.get("routingKey"));
    }

    @Bean("mailMqProperties")
    public MqProperties mailMqProperties(){
        return new MqProperties(mail.get("queue"),mail.get("exchange"),mail.get("routingKey"));
    }

}

在使用的类上面,去掉注解

@EnableConfigurationProperties({UserMqProperties.class})

如下:

package com.wm.contextinit.service;

import com.wm.contextinit.config.MqProperties;
import com.wm.contextinit.config.UserMqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 * @ClassName: TestService
 * @Description:
 * @Author: wm_yu
 * @Create_time: 14:19 2019-12-18
 */
@Service
@Slf4j
public class TestService {

    @Autowired
    private UserMqProperties userMqProperties;

    @Autowired
    private MqProperties logMqProperties;


    @Autowired
    private MqProperties mailMqProperties;

    public void test(){
        log.info("userMqProperties:[{}],logMqProperties:[{}],mailMqProperties:[{}]",userMqProperties.toString(),logMqProperties.toString(),mailMqProperties.toString());
    }



}

再次测试,结果一样的,关键点就是使用Map来接收这个属性

 

引用别人的接收数据的几种情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值