Spring annotation: problems encountered in the use of @Value

Recently I tried to build a framework of spring+springmvc+mybatis+activiti and encountered many problems. I also learned a lot by myself. Record some questions about the use of @Value here.

Regarding the use of @Value in spring, I mainly use it to conveniently refer to the key value of the property file. How to use, there are many related articles on the Internet, which are slightly sorted out. The general usage is as follows:

1. Declare the bean of the spring-loaded property file in the spring configuration file. You need to specify the specific location of the properties file:

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

 This is what I've seen online.

I use another one, using the context tag, which feels more concise, as follows:

<context:property-placeholder location="classpath:resources.properties"/>

 2. The above is the declaration of loading the property file through spring, and the next step is to use it. Annotated with @Value. There also seems to be two ways, the first one:

	@Value("${document.location}")
	private String filePath;

 document.location is the property name in .properties. The annotation assigns the attribute value to the variable filePath. One problem is that I did not add the set method to this class.

Another way is as follows:

@Value("#{configProperties['document.location']}")  
private String filePath;

 

 configProperties is the id of the bean that spring processes the properties file. I don't know if this statement is correct.

None of the above is actually what I wanted to say. What I want to say is that I can't get the value of tm! ! Either null: "${}" is used to obtain the value in the mvc configuration file, and the string ${document.location} is obtained. Either use @Value("${document.location}") in the code to get the value, and get it as null;

 

 

I have also combined the articles of the great gods.

The value is null for the following reasons:

1. The tagValue is modified with static or final, which means that my "filePath" variable cannot be modified by static or final.

2. The class where the "filePath" variable is located must be a class managed by spring. For example, add the @Component annotation.

3. The class instance object where the "filePath" variable is located cannot be new. In fact, the same principle as above.

Reference link: http://blog.csdn.net/zzmlake/article/details/54946346

 

But......the above is not what I have encountered.

 

No way, I honestly looked back at my configuration file, and suddenly found that it is completely normal to use "${}" in the context (spring container) configuration file. Putting it in the mvc (mvc container) configuration file will not work. and mine

<context:property-placeholder location="classpath:resources.properties"/>

It is written in the configuration of the spring container. It stands to reason that mvc inherits from context (I don't know if this statement is correct), and it should be able to take it.

However, after I added the above configuration to the configuration of the mvc container, the value is normal. I am also helpless.

I don't know if there is a standard unified approach. I also hope that God will leave a message.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305727&siteId=291194637