spring中@value注解须要注意

首先,@value须要参数,这里参数能够是两种形式:@Value("#{configProperties['t1.msgname']}")或者@Value("${t1.msgname}");
其次,下面咱们来看看如何使用这两形式,在配置上有什么区别:
一、@Value("#{configProperties['t1.msgname']}")这种形式的配置中有“configProperties”,其实它指定的是配置文件的加载对象:配置以下:
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/config/t1.properties</value>
            </list>
        </property>
    </bean>
    这样配置就可完成对属性的具体注入了;


    二、@Value("${t1.msgname}")这种形式不须要指定具体加载对象,这时候须要一个关键的对象来完成spring

@Value("${t1.msgname}")spa

PreferencesPlaceholderConfigurer,这个对象的配置能够利用上面配置1中的配置,也能够本身直接自定配置文件路径。
    若是使用配置1中的配置,能够写成以下状况:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"/>
    </bean>


    若是直接指定配置文件的话,能够写成以下状况:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="location">
        <value>config/t1.properties</value>
        </property>
    </bean>对象

相关文章
相关标签/搜索