如何在Spring 5.3.10中通过@PropertySource将.properties文件内容注入至Properties/Map类型对象
我明白你想要的是不用额外编写PropertiesFactoryBean配置类、也不依赖Environment对象,直接通过@PropertySource把整个properties文件的内容注入到Map或Properties对象里的简便方法。这里有两种完全符合你需求的方案:
方案一:使用@Value + Spring SpEL直接注入
这种方法不需要额外的配置类,直接在你的Repository类里通过@Value结合Spring表达式语言(SpEL)加载指定的properties文件,注入到Properties或Map<String, String>类型的字段中。
注入为Properties类型
import org.springframework.core.io.support.PropertiesLoaderUtils; // 其他必要导入 @Repository @RequiredArgsConstructor @PropertySource("classpath:database/queries.properties") public class ThisJdbc implements IThisJdbc { // 直接加载指定properties文件到Properties对象 @Value("#{T(org.springframework.core.io.support.PropertiesLoaderUtils).loadAllProperties('classpath:database/queries.properties')}") private Properties sqlStore; @Override public int[] batchInsert(final List<This> these) { String sql = sqlStore.getProperty("batch-insert"); // ...业务逻辑实现 } }
注入为Map<String, String>类型
如果你更倾向于使用Map,可通过SpEL将Properties转换为String类型的键值对Map:
import org.springframework.core.io.support.PropertiesLoaderUtils; import java.util.stream.Collectors; // 其他必要导入 @Repository @RequiredArgsConstructor @PropertySource("classpath:database/queries.properties") public class ThisJdbc implements IThisJdbc { // 加载并转换为String类型的Map @Value("#{T(org.springframework.core.io.support.PropertiesLoaderUtils).loadAllProperties('classpath:database/queries.properties').entrySet().stream().collect(java.util.stream.Collectors.toMap(k -> k.key.toString(), v -> v.value.toString()))}") private Map<String, String> sqlStore; @Override public int[] batchInsert(final List<This> these) { String sql = sqlStore.get("batch-insert"); // ...业务逻辑实现 } }
说明:PropertiesLoaderUtils是Spring核心包提供的工具类,专门用于加载properties文件,不需要额外依赖,Spring Framework 5.3.10完全支持。
方案二:使用@ConfigurationProperties(无前缀绑定)
如果你希望更优雅地管理这些SQL查询,且需要在多个类中复用,可以创建一个简单的配置类,结合@ConfigurationProperties实现无前缀的Map绑定,避免在Repository中写冗长的SpEL。
步骤1:创建配置类
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.util.Map; @Configuration @PropertySource("classpath:database/queries.properties") @ConfigurationProperties // 无前缀,直接绑定所有属性到Map public class SqlQueryProperties { private Map<String, String> queries; // Getter和Setter public Map<String, String> getQueries() { return queries; } public void setQueries(Map<String, String> queries) { this.queries = queries; } }
步骤2:在Repository中注入
@Repository @RequiredArgsConstructor public class ThisJdbc implements IThisJdbc { private final SqlQueryProperties sqlQueryProperties; @Override public int[] batchInsert(final List<This> these) { String sql = sqlQueryProperties.getQueries().get("batch-insert"); // ...业务逻辑实现 } }
说明:这种方式需要确保你的项目中引入了Spring Boot Configuration Processor(如果是Spring Boot项目),或者Spring Framework的spring-context-support包,不过Spring Framework 5.3.10默认已经支持@ConfigurationProperties的基础绑定。
注意事项
- 确保
@PropertySource指定的路径正确,classpath:前缀表示从类路径根目录查找文件。 - 如果properties文件中有特殊字符或空格,建议用双引号包裹值,避免解析错误。
- 方案一中的SpEL表达式虽然直接,但如果properties文件路径需要复用,建议提取为常量或使用配置类管理。
内容的提问来源于stack exchange,提问作者user4695271

