---
title: "使用spring读取properties文件"
author: "zus"
date: "2015-12-24T14:10:17Z"
tags: []
categories: ["企业架构"]
slug: "spring-properties"
draft: false
lastmod: "2015-12-24T14:10:17Z"
id: 2266189
url: "https://zus.iteye.com/blog/2266189"
---

spring可以在配置文件中使用properties文件中配置的项，但是有时候我们也需要直接通过代码去存取配置文件中的项，该怎么做呢？

其实也很简单，spring提供了`PropertiesLoaderUtils`类，此类可以方便的载入properties配置文件，如下实例代码：

```
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


Resource resource = new ClassPathResource("/my.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
```