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 :
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。