spring - 如何在 Spring Boot 中从 [] 读取 List<String> 属性

标签 spring spring-boot properties

有时我看到有人在属性文件中这样定义:

spring.autoconfigure.exclude[0]=com.vietnam.AAutoConfiguration
spring.autoconfigure.exclude[1]=com.vietnam.BAutoConfiguration

只是问题:如何在 Spring bean 中定义一个属性来收集此配置作为列表? 在其他地方他们说要这样使用: 在属性中:

spring.autoconfigure.exclude=com.vietnam.AAutoConfiguration,com.vietnam.BAutoConfiguration

在 Spring Bean 中

@Value("#{'${spring.autoconfigure.exclude}'.split(',')}"

但我不喜欢这种方式,以防值很长。每行一个配置将更容易阅读和维护

谢谢

最佳答案

您可以读取多个字符串,如下所示 在应用程序属性中

app.names=vipul,uncle,bob

在你的组件类中

@Value("${app.names}")
private List<String> names;

关于spring - 如何在 Spring Boot 中从 [] 读取 List<String> 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67226281/

相关文章:

java - Spring boot 将 JSON 反序列化为多态类型

java - Spring @Value 注解方法,当属性不可用时使用默认值

c++ 获取 Boost::Graph 的顶点属性

c# - 这段代码在 C# 中做了什么,它的目的是什么?

java - 如何为 JSP 配置 spring boot mvc 应用程序?

java - Velocity 模板无法解析和显示 html 格式的数据

spring-boot - 我不知道为什么会这样。 jedis 创建名称为 'jedisConnectionFactory' 的 bean 时出错

java - ActiveMQ持久化: some troubles with AMQ Message Store

java - Spring 休息模板

c# - 泛型类的方法看不到子类的 'new' 属性