Spring @Value annotation uses ${} for injection

Original text: http://my.oschina.net/js99st/blog/632104
The new @value annotation in spring3 http://bijian1013.iteye.com/blog/2024068 , here

is the reason why the controller cannot be read successfully : http://cdn.verydemo.com/demo_c143_i7726.html

I have been using the following format injection in previous projects:
@Value("#{config['redis.host']}")
 private String  redisHost;

Injection using the following method is always unsuccessful.
@Value("${redis.host}")
 private String  redisHost;

Recently I finally found out that the statement of the scan configuration file:
<context:property-placeholder location="classpath*:/xxx.properties" />

If you want to send it to the DispatcherServlet of springMVC to scan, instead of the listener ContextLoaderListener of spring to scan, it is more convenient to use "${xxx}" to inject. Detailed configuration: Spring MVC reads the .properties configuration content through the @Value annotation, @value.properties Step 1: Configure in applicationContext.xml: 1 <bean id="configProperties" class="org.springframework.beans.factory.config .PropertiesFactoryBean"> 2 <property name="locations"> 3 <list> 4 <value>classpath:/config/*.properties</value> 5 </list> 6 </property> 7 </bean> 8 < bean id=" propertyConfigurer " class="org.springframework.beans.factory.config.


















9 <property name="properties" ref="configProperties" />
10 </bean>   
Step 2:

Create configuration file content:

For example: userPageSize=5

Step 3:

Use annotations in Controller to get configuration item content:

1 @ Value("#{ configProperties ['userPageSize']}")
2 private String userPageSize;
Step 4:

The following code can use the private variable userPageSize, the value of this string is 5 configured in our configuration file.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058934&siteId=291194637
Recommended