RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Spring表达式语言SpEL用法详解

这篇文章主要介绍了spring表达式语言SpEL用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、璧山网站维护、网站推广。

(1)spring表达式语言是一个支持运行时查询和操作对象图得我强大表达式语言。

(2)语言类似于EL:SpEL使用#{...}作为定界符。所有在大括号中的字符串均被认为是SpEL。

(3)SpEL为bean的属性进行动态赋值提供了便利。

(4)通过SpEL可以实现:

  • 通过Bean的id对Bean进行引用
  • 调用方法及引用对象的属性
  • 计算表达式的值
  • 正则表达式匹配

字面量的表示:

  • 整型:#{5}
  • 小数:#{3.45}
  • 科学计数法:#[1e4]
  • 字符串:可以使用单引号或双引号早味字符串的定界符:#{'tom'},#{"tom"}
  • 布尔值:#{false}

SpEL支持的运算符号:+ - * / % ^ < > == <= >= lt gt eq le ge and or not | (?:) if-else 正则表达式等等

调用静态属性和方法:通过T()

以下代码均忽略类中的getter、setter和to_string方法。

Car.java

package com.gong.spring.beans.spel;
public class Car {
 public Car() {
 }
 public Car(String name) {
  this.name = name;
 }
 private String name;
 //轮胎周长
 private double tyrePerimeter;
 private double price;}

Address.java

package com.gong.spring.beans.spel;

public class Address {
 private String city;
 private String street;
}

Student.java

package com.gong.spring.beans.spel;

public class Student {
 
 private String name;
 private int age;
 private double score;
 private String city;
 private String info;
 private Car car;
 private Address address;
}

beans-spel.xml

<?xml version="1.0" encoding="UTF-8"?>


 
 

  
  
  
  
  
  
  
 

Main.java

package com.gong.spring.beans.spel;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
 public static void main(String[] args) {
  //1.创建spring的IOC容器对象
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml");
  //2.从容器中获取Bean实例
  Student student = (Student) ctx.getBean("student");
  System.out.println(student.toString());
 }
}

部分输出:

Spring表达式语言SpEL用法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前文章:Spring表达式语言SpEL用法详解
文章起源:http://sczitong.cn/article/ipdeih.html