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'

猜你喜欢

转载自www.cnblogs.com/exmyth/p/10442071.html