Spring配置文件中條件判斷標簽


<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<array>
			<value>classpath:/settings.properties</value>
		</array>
	</property>
</bean>

 

 

 

 

<constructor-arg name="password" value="#{'${redis.password}'?:null}"></constructor-arg>
<!--表示當redis.password不為空時,那么返回redis.password,否則返回null -->
<!-- 或者可以如下表示那么含義就更清晰了-->
<constructor-arg name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}"></constructor-arg>

setting.properties:

 

redis.masterName=mymaster
redis.password=
redis.number=0

 

 

 

注意:一定要給${redis.password}顯式加上單引號,否則它就會被解析為bean。從而報錯:

 

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'foobared' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM