第一步:
在applicationContext.xml配置:
1 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 2 <property name="locations"> 3 <list> 4 <value>classpath:/config/*.properties</value> 5 </list> 6 </property> 7 </bean> 8 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> 9 <property name="properties" ref="configProperties" />10 </bean>
第二步:
建立配置文件內容:
例如:userPageSize=5
第三步:
在Controller中使用注解獲得配置項內容:
1 @Value("#{configProperties['userPageSize']}")2 private String userPageSize;
第四步:
后面的代碼就可以使用userPageSize這個私有變量了,這個字符串的值就是我們配置文件中配置的5.
新聞熱點
疑難解答