Spring MVC 通過@Value注解讀取.properties配置內容


  1. 在applicationContext.xml配置:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/config/*.properties</value>
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
</bean>

  2. 建立配置文件properties內容:

 
userPageSize=5

  3. 在Controller中使用注解獲得配置項內容:

 
 
@Value("#{configProperties['userPageSize']}")
private String userPageSize;

  4. 后面的代碼就可以使用userPageSize這個私有變量了 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM