在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件中的文件,进行键值对的注入,例子如下:
目录
-
首先在applicationContext.xml中加入:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:valueTest.properties</value></list></property> </bean> -
新建valueTest.properties文件
name=redgo age=21 -
在Controller , Service中使用
@Value("#{configProperties['name']}") private String name; @Value("#{configProperties.age}") private String age; - 运行测试