从属性文件读取列表并使用Spring注解@Value加载
技术背景
在Java开发中,我们常常需要从属性文件中读取配置信息。当配置信息为列表形式时,如何方便地将其加载到应用程序中是一个常见的需求。Spring框架提供了@Value
注解,可用于注入属性文件中的值,但默认情况下不能直接将逗号分隔的字符串转换为列表。因此,需要采用一些方法来实现这一功能。
实现步骤
方法一:使用Spring EL表达式
- 在属性文件(如
application.properties
)中定义列表值:
1
| my.list.of.strings=ABC,CDE,EFG
|
- 在Java类中使用Spring EL表达式将属性值转换为列表:
1 2 3 4 5 6 7 8 9
| import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List;
@Component public class MyClass { @Value("#{'${my.list.of.strings}'.split(',')}") private List<String> myList; }
|
方法二:激活Spring的ConversionService
- 在Spring配置文件(如
applicationContext.xml
)中添加以下配置:
1 2
| <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" />
|
如果使用Java配置,可在配置类中添加以下方法:
1 2 3 4 5 6 7 8 9 10 11 12
| import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.ConversionService;
@Configuration public class AppConfiguration { @Bean public ConversionService conversionService() { return new DefaultConversionService(); } }
|
- 在Java类中直接使用
@Value
注解注入列表:
1 2 3 4 5 6 7 8 9
| import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List;
@Component public class MyClass { @Value("${my.list.of.ints}") private List<Integer> myList; }
|
同时,在属性文件中定义相应的值:
1
| my.list.of.ints= 1, 2, 3, 4
|
方法三:使用Spring Boot的配置属性
- 在属性文件中按索引定义列表值:
- 创建配置属性类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List;
@Component @ConfigurationProperties("email") public class EmailProperties { private List<String> sendTo;
public List<String> getSendTo() { return sendTo; }
public void setSendTo(List<String> sendTo) { this.sendTo = sendTo; } }
|
- 在需要使用的类中注入配置属性:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
@Component public class EmailModel { @Autowired private EmailProperties emailProperties;
public void useSendToList() { System.out.println(emailProperties.getSendTo()); } }
|
方法四:使用数组转换
- 在属性文件中定义列表值:
1
| my.list.of.strings=ABC,CDE,EFG
|
- 在Java类中使用
@Value
注解注入字符串数组,并转换为列表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import org.springframework.util.CollectionUtils;
@Component public class MyClass { @Value("${my.list.of.strings}") private String[] myString; private List<String> myList;
public MyClass() { myList = Arrays.asList(myString); } }
|
核心代码
以下是使用Spring EL表达式的完整示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List;
@Component public class MyClass { @Value("#{'${my.list.of.strings}'.split(',')}") private List<String> myList;
public List<String> getMyList() { return myList; } }
|
属性文件application.properties
:
1
| my.list.of.strings=ABC,CDE,EFG
|
最佳实践
- 选择合适的方法:根据项目的实际情况选择合适的方法。如果是Spring Boot项目,建议优先考虑使用Spring Boot的配置属性方式;如果是传统Spring项目,可根据需求选择Spring EL表达式或激活
ConversionService
的方式。 - 处理空值和空格:在使用Spring EL表达式时,要注意处理空值和空格。可以使用
trim()
和正则表达式去除多余的空格。 - 配置默认值:在使用
@Value
注解时,可以为属性配置默认值,以防止属性文件中未定义该属性时出现异常。
常见问题
- EL表达式异常:如果在使用Spring EL表达式时出现
org.springframework.expression.spel.SpelEvaluationException
异常,可能是EL表达式书写有误,需要检查表达式的语法。 - 转换失败:如果激活
ConversionService
后仍然无法将属性值转换为列表,可能是ConversionService
未正确配置,需要检查配置文件或Java配置类。 - 空格问题:使用Spring EL表达式时,默认不会去除逗号分隔值中的空格。可以使用正则表达式或
trim()
方法去除多余的空格。