`

一步一步升级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},读不到这个属性,需要用 [] 方括号括起来

分享到:
评论

相关推荐

    spring4示例代码

    spring-1 演示了使用setter方式及构造器方式创建bean,util:list标签创建集合,p标签简化配置 和依赖注入, 以及bean的autowire和继承与依赖,以及bean的作用域。 spring-2 演示了外部配置文件的引入(connection)...

    Spring cloud function SpEL RCE批量检测脚本,反弹shell脚本

    Spring Cloud Function 是基于Spring Boot 的函数计算框架,它抽象出所有传输细节和基础架构,允许开发人员保留所有熟悉的工具和流程,并专注于业务逻辑。 批量检测脚本:python Spel_RCE_POC.py url.txt 反弹...

    Spring_SpEl表达式使用用例

    Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看

    spring spEL 表达式详解

    spring spEL 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖

    krislinzhao#StudyNotes#3.6.使用SpEL表达式加载配置项1

    一、创建Spring Boot应用 二、SpEL结合@Value注解读取配置文件属性 三、SpEL结合 @Value注解读取系统环境变量 四、配置文件的占位符

    SIA实战(一):SpEL的使用

    SIA实战(一):SpEL的使用 源码

    templating:使用spel进行简单的模板制作

    模板化 使用spel进行简单的模板制作

    Spring Data JPA从入门到精通

    内容包括整体认识JPA、JPA基础查询方法、定义查询方法、注解式查询方法、@Entity实例里面常用注解详解、JpaRepository扩展详解、JPA的MVC扩展REST支持、DataSource的配置、乐观锁、SpEL表达式在SpringData里面的应用...

    尚硅谷佟刚Spring4代码及PPT.rar

    代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...

    SSH笔记-SpEL

    SSH笔记-Spring表达式语言:SpEL,关于SpEl的字面量、引用 Bean、属性和方法、支持的运算符号

    Spring Cloud Gateway Actuator API SpEL表达式注入命令执行 0day 漏洞复现

    Spring Cloud Gateway Actuator API SpEL表达式注入命令执行 0day 漏洞复现

    Spring 3.0-API 参考手册

    Spring 3.0-API 参考手册 ...SpEL可以更好的与XML配置文件进行交互,对于安全和集成方面也很有帮助。开发者的工作将更加轻松:原本需要20行Java代码的工作,通过一行SpEL便能解决。” Spring 3.0最新的API与大家分享

    spring4框架系列 [ 4 ]

    spring4框架系列 [ 4 ] 基于xml注入包含setter、构造、命名空间、集合属性、byName、byType、SPEL、匿名Bean、同类抽象、异类抽象、配置多子配置文件

    spring framework4

    spring4 开发jar包 Introduction The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A ...

    spring-framework-reference 3.0

    SpEL可以更好的与XML配置文件进行交互,对于安全和集成方面也很有帮助。开发者的工作将更加轻松:原本需要20行Java代码的工作,通过一行SpEL便能解决。” Spring 3.0最新的API与大家分享 希望能给大家带来很大的...

    Spring表达式语言中文参考手册.docx

    Spring Expression Language (SpEL)中文文档。基于Spring4.x。

    day38 13-Spring的Bean的属性的注入:SpEL注入

    NULL 博文链接:https://364232252.iteye.com/blog/2369770

    Spring核心注解深入解析:提升开发效率

    Spring框架是Java开发中广泛使用的一个强大工具,它通过依赖注入和控制反转等核心概念,极大地提高了开发效率和应用的可维护性。在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@...

    spring3.0帮助文档(包含REST资料)

    ◆富Portlet 2.0支持:Spring MVC完全支持Portlet 2.0环境以及Portlet 2.0中新的事件与资源请求模型。 ◆对象/XML映射(OXM):原本在Spring Web服务中提供,现在被纳入Spring框架核心。 ◆下一代计划能力:新的...

Global site tag (gtag.js) - Google Analytics