1、在applicationContext.xml文件(或引用文件中)中配置properties文件
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:config.properties</value> </array> </property> </bean>
2、在bean中使用@value注解获取配置文件的值
@Value("${ftp.name}") private String ftpName;
注意:
1.即使给变量赋了初值也会以配置文件的值为准
2.用@Value注解只能,在spring管理的bean中使用,在非spring管理的工具类中,只能通过静态方法读取配置文件