likes
comments
collection
share

Spring Boot「09」Property 高级特性

作者站长头像
站长
· 阅读数 12

《设置并使用 Property》中,我们学习了 Spring / Spring Boot 时如何处理外部配置文件及如何在应用中使用配置文件中的 Property。 今天,我们将进一步学习 Spring Boot 中与 Property 使用相关的高级特性。

01-@ConfigurationProperties注解

在定义 Property 时,一个常用的做法是通过公共前缀对某一类相关地 Property 进行区分,例如下面的配置:

example.database.mysql.url=jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true
example.database.mysql.user=user
example.database.mysql.password=password

example.database.redis.host=localhost
example.database.redis.port=6379
example.database.redis.database=1

这种定义 Property 的方式,在 Spring 中被称为层次化属性(hierarchical properties)。 Spring Boot 中定义了一个注解@ConfigurationProperties + @Configuration来帮助开发者方便地将层次化的属性绑定到多个 POJO 中,例如:

@Configuration
@ConfigurationProperties(prefix = "example.database.mysql")
public class MysqlConfigProperties {
    private String url;
    private String user;
    private String password;
}
@Configuration
@ConfigurationProperties(prefix = "example.database.redis")
public class RedisConfigProperties {
    private String host;
    private Integer port;
    private Integer database;
}

除了上述这种方式,还可以通过在 *Application 类上标注@EnableConfigurationProperties,并通过其 value 属性来指定@ConfigurationProperties标注的 POJO 类,例如:

/** 等价于上述 @ConfigurationProperties + @Configuration 的方式 */
@ConfigurationProperties(prefix = "example.database.mysql")
public class MysqlConfigProperties { /** ... */ }

@ConfigurationProperties(prefix = "example.database.redis")
public class RedisConfigProperties { /** ... */ }

@SpringBootApplication
@EnableConfigurationProperties({MysqlConfigProperties.class, RedisConfigProperties.class})
public class PropertiesApplication { /** ... */ }

另外还有一种方式,使用 Spring Boot 2.2 及以上版本的应用中,还可通过@ConfigurationPropertiesScan来自动扫描特定包下标有@ConfigurationProperties注解的类。 若不指定包名,则默认扫描@ConfigurationPropertiesScan标注的类所在包下所有的标注@ConfigurationProperties的类或@Bean方法。

注:通过ConfigurationProperties向 POJO 对象中注入值时以来 POJO 类中的 setters 因此,若无对应 setter,Spring Boot 是无法将属性注入到 Bean 中的。

除了标注在 class 上,该注解还可以标注在@Bean方法上,例如:

@ConfigurationProperties(prefix = "external")
@Bean
public ExternalProperties external() {
    return new ExternalProperties();
}

当标注在@Bean方法上时,如果对应类没有 setter 方法,则会抛 ConfigurationPropertiesBindException 异常。 可以通过@ConfigurationProperties(ignoreInvalidFields = true)来跳过此类错误。

@ConfigurationProperties支持 Property 嵌套,包括 List / Map / Class,例如:

nested.mail.addresses[0]=samson@mail.com
nested.mail.addresses[1]=foo@mail.com
nested.mail.addresses[2]=bar@mail.com

nested.mail.contacts.firstname=samson
nested.mail.contacts.lastname=bu

nested.mail.external.foo=bar
nested.mail.external.bar=foo
@ConfigurationProperties(prefix = "nested.mail")
public class NestedProperties {
    private List<String> addresses;
    private Map<String, String> contacts;
    private ExternalProperties external;
}

02-@Value注解

@Value是 spring-beans 中提供的一个注解,用于向托管在 Spring 容器中的 Bean 的属性注入值。 该注解可以标注在属性、类构造器或类方法上。 该注解仅包含一个 value 属性,value 的值可以是:

  • plain string,例如 "Hello, world!":
@Component
class Demo {
    @Value("Hello, world!")
    private String str;
}
  • property placeholder,例如 "${example.str}":
@Component
class Demo {
    @Value("${example.str}")
    private String str;
}

注:这种写法有个问题,如果 Property example.str 在 Environment 中找不到,则会抛 BeanCreationException 异常, 此时可通过下述默认值的方式,在 Property 不存在时,将默认值赋予类属性

  • property placeholder with default value,例如 "${example.str:Hello, world!}":
@Component
class Demo {
    @Value("${example.str:Hello, world!}")
    private String str;
}

注:如果@Value的目标属性为数组时,Spring Boot 默认以","作为分隔符,例如:

@Value("${external.foo:Hello, world!}")
@Delimiter(value = "#")
private String[] externalFooWithDefaults;

当不使用@Delimiter指定分隔符时,externalFooWithDefaults = {"Hello", "world!"},指定分隔符为"#"后,值为 {"Hello, world!"}

  • SpEL expression。这里不再细究,可以参考官网介绍1

@Value除了能够标注在类属性上,还可以标注在构造器、Setter 方法上:

/** 注解在构造器上 */
private String foo;
public ValueAnnotationDemo(@Value("${external.bar}") String foo) {
    this.foo = foo;
}
/** 注解在 setter 上 */
public void setFoo(@Value("${external.foo}") String foo) {
    this.foo = foo;
}

我们知道 Spring Context 是支持分层的,即 Context 之间可以具有父子关系。 在 parent-context 定义的属性,在 parent-context 和 child-context 中都可以访问,即@ValueEnvironment#getProperty()是可以去到值的; 在 child-context 定义的属性,在 child-context 中@ValueEnvironment#getProperty()是可以访问的;在 parent-context 中两种方式都不可访问;

refs

转载自:https://juejin.cn/post/7156211906373419021
评论
请登录