Spring中的@Value讀取并注入配置信息

@Value不僅可以讀取配置文件中的信息 還可以使用SpEL進行運算操作

The first step

文件:musicer.properties
內容:
musicer.age=56
musicer.song=my heart will go on

The second step

Spring文件內容
<bean id="musicerProp" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <array>
                <value>classpath:musicer.properties</value>
            </array>
        </property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="musicerProp"/>
</bean>

The third step

@Value("#{musicerProp['musicer.age']}")
private Integer age;
@Value("#{musicerProp['musicer.song']}")
private String song;

結果打印:

age: 56 Playing my heart will go on :
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。