自学内容网 自学内容网

Spring Boot 进阶- 如何从配置文件中获取值?

在这里插入图片描述
 &emps;在上一篇文章中,我们介绍了两种配置文件的方式,那么在添加完配置文件之后,我们如何从配置文件中获取到对应配置的值呢?这篇文章中我们就来看看这个问题。

  一般的说SpringBoot中读取配置文件的方式有两种

  • @Value注解:基于@Value注解进行配置,一般适用于单个属性值的注入,这是一种直接进行注入的方式实现。不需要太多的配置内容
  • @ConfigurationProperties注解:这种配置方式主要是用于在同一个前缀下的多个配置项的时候使用,也是在Spring Boot自动配置中使用的配置方式。

@ConfigurationProperties 配置文件

  这个注解主要是用来从配置文件中获取配置内容,支持基本配置项和复杂配置项,但是不能支持SPEL表达式配置。这个注解源码如下

@Target({
    ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface ConfigurationProperties {
   

/**
 * The prefix of the properties that are valid to bind to this object. Synonym for
 * {@link #prefix()}. A valid prefix is defined by one or more words separated with
 * dots (e.g. {@code "acme.system.feature"}).
 * @return the prefix of the properties to bind
 */
@AliasFor("prefix")
String 

原文地址:https://blog.csdn.net/nihui123/article/details/142544531

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!