Spring配置文件中就没有条件判断标签吗
展开全部
使用SpEL可以满足上述的需求,如下所示:
<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'
<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'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询