2010-02-10 135 views
2

使用Spring 3.0 EL可以自动将属性从属性文件连接到组件吗?Spring 3.0 EL如何从属性文件中获取属性?

我知道我们可以使用Util名称空间加载属性文件,我们可以将它作为bean来访问,但是我们可以使用spring EL自动装入。

<util:properties id="myProperties" location="/WEB-INF/my.properties"/>

+0

你能给什么样的事情会进行布线的例子吗? – skaffman 2010-02-10 15:25:41

+0

我假设一个'String'属性 – Bozho 2010-02-10 15:27:27

+0

我猜Spring EL可以将属性转换为所需的类型。例如,我的属性文件中有一个属性,如'min.age.required = 18'。而且我想把这个连接到'int age';'在我的服务中。如何使用EL来做到这一点。 – 2010-02-10 15:31:29

回答

8
@Value("#{ myProperties['min.age.required'] }") 
int age; 

顺便说一句,好老<context:property-placeholder .../>作品也没关系:

@Value("${min.age.required}") 
int age;