一步一步升级spring配置4:使用SPEL和Util标签配置spring xml参数

 

1.原始做法: spring xml配置文件,参数直接混合配置

如下

 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1/feilong" />
<property name="username" value="feilong_prod" />
<property name="password"><value><![CDATA[feilong]]></value></property>
<property name="maxActive" value="700" />
<property name="maxIdle" value="20" />
<property name="maxWait" value="3000" />
</bean>

 

缺点:参数和bean混在一起,不利于查找编辑发布

 

2.稍微改进,使用context:property-placeholder

 

新建  dataSource.properties

 

######################### dataSource info###################################
dataSource.driverClassName=org.postgresql.Driver
dataSource.url=jdbc:postgresql://10.8.12.207/db_converse
dataSource.username=user_converse
dataSource.password=user_converse1234

dataSource.maxActive=80
dataSource.maxIdle=20
dataSource.maxWait=3000

 

 

 

 

<context:property-placeholder location="classpath*:config/datasource.properties" />   
<!--dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
<property name="maxActive" value="${dataSource.maxActive}" />
<property name="maxIdle" value="${dataSource.maxIdle}" />
<property name="maxWait" value="${dataSource.maxWait}" />
</bean>

 

 

缺点:这个问题困扰我很久,

就是,如果我有的bean 不需要外部参数配置,就想使用 ${} 这样的符号

 

比如

 

<bean id="Sku.findSkuRelationByCategory" class="loxia.dao.support.DynamicQueryHolder">
<constructor-arg>
<value>
<![CDATA[select
r.sku_id as sku_id,
r.sku_category_id as sku_category_id
from t_ma_sc_sku_relation r
where r.sku_id in(#foreach($num in [1..$skuCount]) #if($num == 1) :s${num} #else ,:s${num} #end #end)
and r.sku_category_id in(#foreach($num in [1..$categoryCount]) #if($num == 1) :c${num} #else ,:c${num} #end #end)
order by sku_category_id]]>
</value>
</constructor-arg>
</bean>

 

 

stackoverflow上面,国外的朋友给我解答,虽然我的英文很烂,但是外国人看懂了,开心http://stackoverflow.com/questions/10257448/contextproperty-placeholder-is-a-good-thing-but-i-do-not-want-some-bean-config

 

解决方案:

1).context:property-placeholder使用placeholderPrefixplaceholderSuffix属性

以前不知道这个属性,长见识了,这个方案不错,如果你的spring3.0以下的话,使用这个


2).不使用外部参数,但要用$符号的bean,可以使用SPEL 转义#{'$'}num}

这个方案可以解决问题,但是比较坑爹,我有很多这样的sql,都需要人工转义,太麻烦,抛弃

 

3.使用SPELUtil标签配置spring xml参数

SPEL spring 3.0新的特性

 

<util:properties id="p_dataSource" location="classpath:config/dataSource.properties"></util:properties>

 

<!--dataSource -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="#{p_dataSource['dataSource.driverClassName']}" />

<property name="url" value="#{p_dataSource['dataSource.url']}" />

<property name="username" value="#{p_dataSource['dataSource.username']}" />

<property name="password" value="#{p_dataSource['dataSource.password']}" />

<property name="maxActive" value="#{p_dataSource['dataSource.maxActive']}" />

<property name="maxIdle" value="#{p_dataSource['dataSource.maxIdle']}" />

<property name="maxWait" value="#{p_dataSource['dataSource.maxWait']}" />

</bean>

 

注意:不能配置成#{p_dataSource.dataSource.maxIdle},读不到这个属性,需要用 [] 方括号括起来

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值