iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot 常用读取配置文件的三种方法详解
  • 440
分享到

SpringBoot 常用读取配置文件的三种方法详解

2024-04-02 19:04:59 440人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录前言一、使用 @Value 读取配置文件二、使用 @ConfigurationProperties 读取配置文件1、类上添加@Configuration注解2、使用@Enable

我们在SpringBoot框架进行项目开发中该如何优雅的读取配置呢?或者说对于一些List或者Map应该如何配置呢?

本篇主要解决如下几个问题:

1、Spring Boot有哪些常用的读取配置文件方式?

1)使用 @Value 读取配置文件

2) 使用 @ConfigurationProperties 读取配置文件

3)使用 Environment 读取配置文件

2、一些复杂的数据结构,如List、Map,如何配置?如何读取呢?

前言

spring Boot默认的配置文件有两种格式: application.properties 和 application.yml。 查找顺序是首先从application.properties 查找,

如果找不到,再查找 application.yml。优先级:application.properties > application.yml。

以yml中RabbitMQ的配置为例,配置文件如下:

rabbitMQ:
  host: 127.0.0.1
  passWord: root
  port: 5672
  username: root

一、使用 @Value 读取配置文件

这种方法适用于对象的参数比较少的情况

我们可以直接在对象的属性上使用 @Value 注解,同时以 ${} 的形式传入配置文件中对应的属性。同时需要在该类的上方使用 @Configuration 注解,将该类作为配置

文件加入,在启动项目的时候实现注入。

@Configuration
public class RabbitmqProperties {

    @Value("${rabbitmq.host}")
    private String rabbitmqHost;
    @Value("${rabbitmq.port}")
    private String rabbitmqPort;
    @Value("${rabbitmq.username}")
    private String rabbitmqUsername;
    @Value("${rabbitmq.password}")
    private String rabbitmqPassword;
}

如果哪里需要用到,通过 @Autowired 注入进去就可以获取属性值了

@Component
public class PropertiesTest {

    @Autowired
    private RabbitmqProperties rabbitmqProperties;
}

二、使用 @ConfigurationProperties 读取配置文件

如果对象的参数比较多情况下,推荐使用 @ConfigurationProperties 会更简单一些,不需要在每一个字段的上面的使用@Value注解。

@ConfigurationProperties注解声明当前类为配置读取类

prefix="rabbitmq" 表示读取前缀为rabbitmq的属性

示例如下:

@ConfigurationProperties(prefix = "rabbitmq")
public class RabbitmqProperties {
    private String host;
    private String port;
    private String username;
    private String password;
}

这里有一点需要注意: 必须保证属性名称和字段一模一样,且类需要提供字段的setter方法

注意 如果仅仅只是使用了 @ConfigurationProperties 注解是没有效果的,它并不会将这个配置注入容器中,它还需要和注入容器的注解一起使用。

这里有两种方法实现将它注入到容器中

1、类上添加@Configuration注解

除了@Configuration,也可以是@Controller、@RestController、@Service、@Componet等注解,加入到ioc容器里。

@Setter
@Configuration
@ConfigurationProperties(prefix = "rabbitmq")
public class RabbitmqProperties {
    private String host;
    private String port;
    private String username;
    private String password;
}

同样哪里需要用到,通过 @Autowired 注入进去就可以获取属性值了

2、使用@EnableConfigurationProperties注解

通过 @EnableConfigurationProperties 注解可以将指定的配置读取类的对象加载到Spring容器,也就是说,在其他配置类上使用一个@EnableConfigurationProperties注解,

来将配置文件的参数和RabbitmqProperties类的属性绑定。这样就不需要在RabbitmqProperties类上使用@Configuration注解了

@Configuration
@EnableConfigurationProperties(RabbitmqProperties.class)
public class RabbitmqConfig {

    @Autowired
    private RabbitmqProperties prop;
    
    @Bean
    public Rabbitmq rabbitmq() {
        Rabbitmq mq = new Rabbitmq();
        mq.setHost(prop.getHost());
        mq.setPort(prop.getPort());
        mq.setUsername(prop.getUsername());
        mq.setPassword(prop.getPassword());
        return mq;
    }
}

3、使用@ConfigurationPropertiesScan扫描

在 Spring Boot 2.2.0.RELEASE 中提供了一个扫描注解@ConfigurationPropertiesScan。它可以扫描特定包下所有的被@ConfigurationProperties标记的配置类,

并将它们进行IoC注入。

@ConfigurationPropertiesScan
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

三、使用 Environment 读取配置文件

Environment 是 SprinGCore 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的getProperty方法来获取某个配置项的值。

如下代码所示:

@SpringBootApplication
public class MainApplication implements InitializingBean {
    
    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() {
        String username = environment.getProperty("rabbitmq.username");
        System.out.println("rabbitmq当前用户名为:  " + username);
    }
}

四、常用的几种数据结构配置读取

比如我们常见的字符串、整数、List、Map如何配置和读取呢?这里以之前自己开发的一个项目中用到复杂的配置数据来做示例之前有发布一个基于

Spring Cache实现二级缓存(Caffeine+Redis) 的项目,里面的配置就比较复杂。

具体项目地址:基于Spring Cache实现Caffeine+Redis二级缓存

这里展示配置示例:

application.yml

# 二级缓存配置
l2cache:
  config:
    # 是否存储空值,默认true,防止缓存穿透
    allowNullValues: true
    # 组合缓存配置
    composite:
      # 是否全部启用一级缓存,默认false
      l1AllOpen: false
      # 是否手动启用一级缓存,默认false
      l1Manual: true
      # 手动配置走一级缓存的缓存key集合,针对单个key维度
      l1ManualKeySet:
      - userCache:user01
      - userCache:user02
      - userCache:user03
      - userCache:user04
      
    # 一级缓存
    caffeine:
      # 缓存刷新调度线程池的大小
      refreshPoolSize: 2
      # 写入后过期时间(秒)
      expireAfterWrite: 180
      # 访问后过期时间(秒)
      expireAfterAccess: 180
      # 初始化大小
      initialCapacity: 100
      # 最大缓存对象个数,超过此数量时之前放入的缓存将失效
      maximumSize: 300
      
    # 二级缓存
    redis:
      # 全局过期时间,单位毫秒,默认不过期
      defaultExpiration: 300000
      # 每个cacheName的过期时间,单位毫秒
      expires: {userCache: 300000,GoodsCache: 50000}
      # 缓存更新时通知其他节点的topic名称 默认 cache:redis:caffeine:topic
      topic: cache:redis:caffeine:topic

配置实体类

@Data
@ConfigurationProperties(prefix = "l2cache")
public class L2CacheProperties {
    
    private L2CacheConfig config;
}

L2CacheConfig


@Data
public class L2CacheConfig {


    
    private boolean allowNullValues = true;
    
    
    private final Composite composite = new Composite();
    
    
    private final Caffeine caffeine = new Caffeine();

    
    private final Redis redis = new Redis();

    
    @Data
    public static class Composite {

        
        private boolean l1AllOpen = false;

        
        private boolean l1Manual = false;

        
        private Set<String> l1ManualKeySet = new HashSet<>();
    }
    
    
    @Data
    public static class Caffeine {
  
        
        private Integer refreshPoolSize = Runtime.getRuntime().availableProcessors();

        
        private long expireAfterWrite;

        
        private long refreshAfterWrite;

        
        private int initialCapacity;

        
        private long maximumSize;
    }

    
    @Data
    public static class Redis {

        
        private long defaultExpiration = 0;

        
        private Map<String, Long> expires = new HashMap<>();

        
        private String topic = "cache:redis:caffeine:topic";

    }
}

Configuration类

@Configuration
@EnableConfigurationProperties(L2CacheProperties.class)
public class CacheRedisCaffeineAutoConfiguration {

   @Autowired
   private L2CacheProperties l2CacheProperties;

   @Bean
   public RedisCaffeineCacheManager cacheManager() {
      return new RedisCaffeineCacheManager(l2CacheProperties.getConfig());
   }
}

测试

项目启动后,进行Debug打断点,看有木有注入到L2CacheProperties实体中

通过上面截图就可以看出,所有application.yml配置数据,都已经在L2CacheProperties实体中,说明配置成功,获取也成功了。

到此这篇关于SpringBoot 常用读取配置文件的 3 种方法! 的文章就介绍到这了,更多相关SpringBoot 读取配置文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot 常用读取配置文件的三种方法详解

本文链接: https://www.lsjlt.com/news/168565.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作