spring配置文件中的条件表达式

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:/settings.properties</value>
			</array>
		</property>
	</bean>
	<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
		<constructor-arg name="masterName" value="${redis.masterName}"></constructor-arg>
		<constructor-arg name="sentinels">
			<set>
				<value>${redis.host.a}</value>
				<value>${redis.host.b}</value>
				<value>${redis.host.c}</value>
			</set>
		</constructor-arg>
		<constructor-arg name="poolConfig">
			<bean class="redis.clients.jedis.JedisPoolConfig"></bean>
		</constructor-arg>
		<constructor-arg name="timeout" value="2000"></constructor-arg>
		<constructor-arg name="password" value="#{'${redis.password}'?:null}"></constructor-arg>
		<constructor-arg name="database" value="${redis.number}"></constructor-arg>
	</bean>
	<bean id="jedisTemplate" class="com.tcl.account.utils.JedisTemplate">
		<constructor-arg name="jedisPool" ref="jedisSentinelPool"></constructor-arg>
	</bean>

settings.properties:

redis.masterName=mymaster
redis.password=
redis.number=0
#test
redis.host.a=127.0.0.1:26379
redis.host.b=127.0.0.1:26379
redis.host.c=127.0.0.1:26379

注意上面spring配置文件中password的赋值,若${redis.password}不为空便返回自身,否则返回null。

另外${redis.password}一定放在单引号内否则会被当做bean id,于是会报这样的错:

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'
	at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:208)




转载于:https://my.oschina.net/zhuguowei/blog/400746

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值