---
title: "Spring使用程序方式读取properties文件"
author: "peihong-ph"
date: "2014-07-03T10:37:35Z"
tags: ["Spring"]
categories: ["编程语言"]
slug: "spring-properties"
draft: false
lastmod: "2014-07-03T10:37:13Z"
id: 2087828
url: "https://peihong-ph.iteye.com/blog/2087828"
---

Spring使用程序方式读取properties文件 在spring中可以通过下面的方式将配置文件中的项注入到配置中 classpath\*:application.properties 但是这样的注入没有办法通过程序来访问properties文件中的内容，spring还提供了 org.springframework.core.io.support.PropertiesLoaderUtils类可以方便的载入配置文件，如下两行代码： Resource resource = new ClassPathResource("/application.properties"); Properties props = PropertiesLoaderUtils.loadProperties(resource); 需要引用下面的类： import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils;