码迷,mamicode.com
首页 > 编程语言 > 详细

spring SpEL--转

时间:2015-12-10 19:26:22      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

原文:http://www.tuicool.com/articles/Jbq2QnM

概要:

技术分享

Spring表达式语言:SpEL

  • Spring表达式语言 (简称 SpEL ):是一个 支持运行时查询和操作对象图的强大的表达式语言
  • 语言类似于EL:SpEL使用#{...} 作为定界符,所有在大括号中的字符都将被认为是SpEL
  • SpEL为bean的属性进行动态赋值提供了便利
  • 通过SpEL可以实现:
    • 通过bean的id对bean进行引用
    • 调用方法以及引用对象中的属性
    • 计算表达式的值
    • 正则表达式的匹配 

SpEL:字面量(仅赋予字面值,使用SpEL的意义不大)

  • 字面量的表示
    • 整数:<property name="count" value=" #{5} "/>
    • 小数:<property name="frequency" value=" #{89.7} "/>
    • 科学计算法:<property name="capacity" value=" #{1e4} "/>
    • String可以使用单引号或者双引号作为字符串的定界符号 :<property name="name" value=" #{‘Chuck’} "/>或<property name=‘name‘ value=‘ #{"Chuck"} ‘/>
    • Boolean:<property name="enabled" value=" #{false} "/>

SpEL:引用Bean、属性和方法

  • 引用其他对象:
  • 技术分享
  • 引用其他对象的属性(用普通的方式做不到的)
  • 技术分享
  • 调用其他方法,还可以链式操作
  • 技术分享
  • 调用静态方法或静态属性 :通过T()调用一个类的静态方法,它将返回一个Class Object,然后再调用相应的方法或属性:技术分享

SpEL支持的运算符号

  • 算数运算符:+,-,*,/,%,^
  • 技术分享
  • 加号还可以用作字符串连接:
  • 技术分享
  • 比较运算符:<,>,==,<=,>=,lt,gt,eq,le,ge
  • 技术分享
  • 逻辑运算符号:and,or,not,|
  • 技术分享
  • if-else运算符:?:(temary),?:(Elvis)
  • 技术分享
  • if-else的变体
  • 技术分享
  • 正则表达式:matches
  • 技术分享

实例代码详解

目录结构(用到的包)

技术分享

Address.java

package com.coslay.beans.spel;

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

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getStreet() {
    return street;
  }

  public void setStreet(String street) {
    this.street = street;
  }

  @Override
  public String toString() {
    return "Address [city=" + city + ", street=" + street + "]";
  }

}

Car.java

package com.coslay.beans.spel;

public class Car {
  private String brand;
  private double price;
  // 轮胎的周长
  private double tyrePerimeter;

  public String getBrand() {
    return brand;
  }

  public void setBrand(String brand) {
    this.brand = brand;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public Car() {
    System.out.println("Car‘s Constructor...");
  }

  public double getTyrePerimeter() {
    return tyrePerimeter;
  }

  public void setTyrePerimeter(double tyrePerimeter) {
    this.tyrePerimeter = tyrePerimeter;
  }

  @Override
  public String toString() {
    return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter="
        + tyrePerimeter + "]";
  }

}

Person.java

package com.coslay.beans.spel;

public class Person {
  private String name;
  private Car car;
  // 引用address bean的city属性
  private String city;
  // 根据car的price确定info:car的price》=300000:金额
  // 否则为:白领
  private String info;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Car getCar() {
    return car;
  }

  public void setCar(Car car) {
    this.car = car;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getInfo() {
    return info;
  }

  public void setInfo(String info) {
    this.info = info;
  }

  @Override
  public String toString() {
    return "Person [name=" + name + ", car=" + car + ", city=" + city
        + ", info=" + info + "]";
  }

}

Main.java

package com.coslay.beans.spel;

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

public class Main {
  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "beans-spel.xml");

    Address address = (Address) ctx.getBean("address");
    System.out.println(address);
    
    Car car = (Car) ctx.getBean("car");
    System.out.println(car);
    
    Person person = (Person) ctx.getBean("person");
    System.out.println(person);
  }
}

beans-spel.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  
  <bean id="address" class="com.coslay.beans.spel.Address">
    <!-- 使用spel为属性赋一个字面值 -->
    <property name="city" value="#{‘beijint‘}"></property>
    <property name="street" value="WuDaoKou"></property>
  </bean> 
  
  <bean id="car" class="com.coslay.beans.spel.Car">
    <property name="brand" value="Audi"></property>
    <property name="price" value="5000000"></property>
    <!-- 使用SpEL引用类的静态属性 -->
    <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
  </bean>
  
  <bean id="person" class="com.coslay.beans.spel.Person">
    <!-- 使用 SpEL来引用其他的Bean -->
    <property name="car" value="#{car}"></property>
    <!-- 使用SpEL来引用其他的Bean的属性 -->
    <property name="city" value="#{address.city}"></property>
    <!-- SpEL中使用运算符 -->
    <property name="info" value="#{car.price > 300000 ? ‘金领‘ : ‘白领‘}"></property>
    <property name="name" value="Tom"></property>
  </bean>
</beans>

spring SpEL--转

标签:

原文地址:http://www.cnblogs.com/mochaMM/p/5036785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!