Spring--6.spEL
来源:互联网 发布:mac六国循环重启 恢复 编辑:程序博客网 时间:2024/06/02 02:46
1 SpEL --Spring表达式语言#
1.1 概念
SpEL是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
SpEL为 bean 的属性进行动态赋值提供了便利
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用
调用方法以及引用对象中的属性
计算表达式的值
正则表达式的匹配
1.2 字面量
整数:<propertyname="count"value="#{5}"/>
小数:<propertyname="frequency"value="#{89.7}"/>
科学计数法:<propertyname="capacity"value="#{1e4}"/>
String可以使用单引号或者双引号作为字符串的定界符号:
<propertyname=“name”value="#{'Chuck'}"/>
或 <propertyname='name'value='#{"Chuck"}'/>
Boolean:<propertyname="enabled"value="#{false}"/>
<bean id="address" class="spring.spel.Address"> <!-- 使用 spel 为属性赋一个字面值 --> <property name="city"value="#{'北京'}"></property> <property name="street"value="#{'天安门'}"></property> </bean>
1.3 SpEL支持的运算符号
算数运算符:+, -, *, /, %, ^:
加号还可以用作字符串连接:
比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
逻辑运算符号: and, or, not, |
if-else 运算符:?: (ternary), ?: (Elvis)
if-else 的变体
正则表达式:matches
1.4 引用 Bean、属性和方法
引用其他对象:
引用其他对象的属性
调用其他方法,还可以链式操作
<bean id="person" class="spring.spel.Person"><!-- 应用其他的 bean --><property name="car" value="#{car}"></property><!-- 应用其他的 bean 的属性--><property name="city" value="#{address.city}"></property><!-- 使用运算符 --><property name="info" value="#{car.price > 300000 ? '金领' : '白领'}"></property><property name="name" value="老兰"></property></bean>
调用静态方法或静态属性:
通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:
<beanid="car"class="spring.spel.Car"> <propertyname="brand"value="奥迪"></property> <propertyname="price"value="500000"></property> <!-- 使用 SpEL引用类的静态属性 --> <propertyname="tyrePerimeter"value="#{T(java.lang.Math).PI * 80}"></property> </bean>
2 加载多个配置文件
2.1 一种:创建工厂的时候加载多个配置文件:
ApplicationContext applicationContext =
newClassPathXmlApplicationContext("applicationContext.xml",
"applicationContext2.xml");
2.2 二种:在一个配置文件中包含另一个配置文件:
<import resource="applicationContext2.xml"></import>
3 使用外部属性文件—连接数据库
在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和Bean 配置相分离
Spring提供了一个PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为${var} 的变量,PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
Spring还允许在属性文件中使用${propName},以实现属性之间的相互引用。
<!-- 导入属性文件 --> <context:property-placeholderlocation="classpath:db.properties"/> <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 使用外部属性文件 ,不要有空格${user }--> <propertyname="user"value="${user}"></property> <propertyname="password"value="${password}"></property> <propertyname="driverClass"value="${driverclass}"></property> <propertyname="jdbcUrl"value="${jdbcurl}"></property> </bean>
- Spring--6.spEL
- spring SpEL
- spring SpEL
- Spring --- SpEL
- Spring SpEL
- Spring - SpEL
- Spring-SpEL
- spring-SpEL
- Spring---SpEL
- Spring Expression Language (SpEL)
- Spring Security3 SpEL表达式
- Spring SpEL表达式
- SPEL-Spring表达式
- spring 的Spel
- spring的SpEl表达式
- Spring SpEL表达式
- spring 使用spel
- Spring-Bean配置-SpEL
- (五)Java设计模式之建造模式
- 聊聊TCP四次挥手中的timewait
- Linux命令简写和全称—方便记忆
- JDK7中ReentrantLock源码解析(3)
- 用webpack工具打包echarts项目
- Spring--6.spEL
- linux查看端口占用情况
- JDK7中ReentrantReadWriteLock源码解析(1)
- Matlab Excel服务器,笔记
- AudioFlinger Binder Struct Chart
- 由三边求三角形面积
- Spring Boot 集成Redis
- 黑板架构风格
- 笨方法学习Python-习题15: 读取文件