---
title: "spring中读取配置属性的方法"
author: "Dragonmandance"
date: "2016-07-14T17:20:09Z"
tags: []
categories: ["编程语言"]
slug: "spring"
draft: false
lastmod: "2016-07-14T17:31:26Z"
id: 2311289
url: "https://dragonmandance.iteye.com/blog/2311289"
---

1、PropertyPlaceholderConfigurer


```java

<bean id="propertyConfigurer" 		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="locations"> 
			<list> 
				<value>xxx.properties</value>
			</list>
		</property> 
	</bean>
location="classpath:test.properties"
<value>classpath:xxx.properties</value>

读取时 value="${name}"

xxx.properties详情

name=aaaaa
age=12

在类中读取properties文件属性
在类中需要注入的属性实现 setter 和 getter 方法。
在 setter 方法前，添加 @Value 注解
@Value("#{propertiesReader[propertiesname]}")
propertiesName 为 properties 文件中的键。这样，在容器启动过程中， Spring 将自动注入值。

```