The @value annotation sets the default value

After using spring 3.0, I saw its new practical @value annotation, and I sorted out the method of setting the default value of the @value annotation.

 

First of all, you need to introduce the properties file in the spring container. The example is as follows:

 

<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>classpath:ttt.properties</value>
			<value>classpath:timerbin.properties</value>
		</list>
	</property>
	<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

 timerbin.properties

 

 

timer.userAge=22

 

 

1. Use the values ​​configured in properties in java code

 

 

@Value("${timer.userAge}")
private int userAge;

 

 

The class of the userAge attribute defined at this time needs to be loaded into the spring container using annotations.

 

 

2. The @Value annotation sets the default value

 

@Value("${timer.userAge:22}")
private int userAge;

 When timer.userAge is not found in properties, it will assign 22 to the userAge property

 

 

Guess you like

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