Note the @value annotation in spring

  • 2020-06-15 09:12:09
  • OfStack

First, @value needs parameters, which can take either @Value ("#{configProperties[' t1.msgname ']}") or @Value ("${t1.msgname}");

Second, let's look at how to use the two forms and what the configuration differences are:

1. @Value("#{configProperties['t1.msgname']}") The configuration in this form is" configProperties In fact, it specifies the loading object of the configuration file: The configuration is as follows:


<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="locations">
     <list>
       <value>classpath:/config/t1.properties</value>
     </list>
   </property>
 </bean> 

This configuration completes the specific injection of attributes;

2, @Value("${t1.msgname}") This form does not need to specify the specific load object, this time needs a key object to complete PreferencesPlaceholderConfigurer , the configuration of this object can take advantage of the configuration in Configuration 1 above, or it can customize the configuration file path itself.

If you use the configuration in Configuration 1, you can write it as follows:


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="properties" ref="configProperties"/>
 </bean> 

If you specify the configuration file directly, you can write it as follows:


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="location">
    <value>config/t1.properties</value>
    </property>
  </bean> 

Related articles: