此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
用@Value
@Value
通常用于注入外部化属性:
-
Java
-
Kotlin
@Component
public class MovieRecommender {
private final String catalog;
public MovieRecommender(@Value("${catalog.name}") String catalog) {
this.catalog = catalog;
}
}
@Component
class MovieRecommender(@Value("\${catalog.name}") private val catalog: String)
使用以下配置:
-
Java
-
Kotlin
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig { }
@Configuration
@PropertySource("classpath:application.properties")
class AppConfig
以及以下内容application.properties
文件:
catalog.name=MovieCatalog
在这种情况下,catalog
parameter 和 field 将等于MovieCatalog
价值。
Spring 提供了默认的宽松嵌入值解析器。它将尝试解析
属性值,如果无法解析,则为属性名称(例如${catalog.name}
)
将作为值注入。如果您想对不存在的
值,您应该声明一个PropertySourcesPlaceholderConfigurer
bean,如下所示
示例显示:
-
Java
-
Kotlin
@Configuration
public class AppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@Configuration
class AppConfig {
@Bean
fun propertyPlaceholderConfigurer() = PropertySourcesPlaceholderConfigurer()
}
配置PropertySourcesPlaceholderConfigurer 使用 JavaConfig 时,@Bean method 必须为static . |
如果无法解析任何占位符,则使用上述配置可确保 Spring 初始化失败。也可以使用${}
setPlaceholderPrefix
,setPlaceholderSuffix
,setValueSeparator
或setEscapeCharacter
以自定义占位符。
默认情况下,Spring Boot 会配置一个PropertySourcesPlaceholderConfigurer bean 那个
将从application.properties 和application.yml 文件。 |
Spring 提供的内置转换器支持允许简单的类型转换(到Integer
或int
)进行自动处理。多个逗号分隔值可以是
自动转换为String
数组。
可以按如下方式提供默认值:
-
Java
-
Kotlin
@Component
public class MovieRecommender {
private final String catalog;
public MovieRecommender(@Value("${catalog.name:defaultCatalog}") String catalog) {
this.catalog = catalog;
}
}
@Component
class MovieRecommender(@Value("\${catalog.name:defaultCatalog}") private val catalog: String)
弹簧BeanPostProcessor
使用ConversionService
在幕后处理
转换String
值@Value
添加到 Target 类型。如果您想
提供自家自定义类型的转换支持,可自家提供ConversionService
bean 实例,如下例所示:
-
Java
-
Kotlin
@Configuration
public class AppConfig {
@Bean
public ConversionService conversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
conversionService.addConverter(new MyCustomConverter());
return conversionService;
}
}
@Configuration
class AppConfig {
@Bean
fun conversionService(): ConversionService {
return DefaultFormattingConversionService().apply {
addConverter(MyCustomConverter())
}
}
}
什么时候@Value
包含一个SpEL
表达该值将为 dynamic
在运行时计算,如下例所示:
-
Java
-
Kotlin
@Component
public class MovieRecommender {
private final String catalog;
public MovieRecommender(@Value("#{systemProperties['user.catalog'] + 'Catalog' }") String catalog) {
this.catalog = catalog;
}
}
@Component
class MovieRecommender(
@Value("#{systemProperties['user.catalog'] + 'Catalog' }") private val catalog: String)
SPEL 还支持使用更复杂的数据结构:
-
Java
-
Kotlin
@Component
public class MovieRecommender {
private final Map<String, Integer> countOfMoviesPerCatalog;
public MovieRecommender(
@Value("#{{'Thriller': 100, 'Comedy': 300}}") Map<String, Integer> countOfMoviesPerCatalog) {
this.countOfMoviesPerCatalog = countOfMoviesPerCatalog;
}
}
@Component
class MovieRecommender(
@Value("#{{'Thriller': 100, 'Comedy': 300}}") private val countOfMoviesPerCatalog: Map<String, Int>)