通过@Value注解读取.properties配置内容

(1)Controller 中 通过@value 读取properties配置信息

1、springmvc-servlet.xml 中配置:


    <!--通过@Value注解读取.properties配置内容-->
    <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、basicmanagement.properties中配置参数

sessionTimeOut = 60  

3、Controller 获取方式

@Value("#{configProperties['sessionTimeOut']}")  
private int sessionTimeOut;  

(二)Service 中 通过@value 读取properties配置信息

1、applicationContext.xml中的配置

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location" value="classpath:basicmanagement.properties" />  
</bean> 

第二种方式

 <!--读取properties配置文件-->
    <context:property-placeholder location="classpath:config.properties"/>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容