【尚硅谷】spring学习笔记(8):SpEL

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

字面量的表示:

  • 整数:
  • 小数:
  • 科学计数法:
  • String可以使用单引号或者双引号作为字符串的定界符号:
  • Boolean:

  • 引用其他对象:
  • 引用其他对象的属性

  • 调用其他方法,还可以链式操作

  • 算数运算符:+, -, *, /, %, ^:

【尚硅谷】spring学习笔记(8):SpEL_第1张图片

  • 加号还可以用作字符串连接:
  • 比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge

代码:

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 + "]";
	}	
}
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 getTyrePerimeter() {
		return tyrePerimeter;
	}
	public void setTyrePerimeter(double tyrePerimeter) {
		this.tyrePerimeter = tyrePerimeter;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]";
	}
	public Car() {
		System.out.println("car的构造器");
	}	
}
public class Person {
	private String name;
	
	private Car car;

	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 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;
	}

	public Car getCar() {
		return car;
	}

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

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



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

public class Main {
	public static void main(String[] args) {
	ApplicationContext apc = new  ClassPathXmlApplicationContext("beans-sqel.xml");
     
		Address address = (Address) apc.getBean("address");
		System.out.println(address);
		
		Car car = (Car) apc.getBean("car");
		System.out.println(car);
		
		Person person = (Person) apc.getBean("person");
		System.out.println(person);
	}
}

结果:

car的构造器
Address [city=BeiJing, street=朝阳路]
Car [brand=jili, price=300000.0, tyrePerimeter=251.32741228718345]
Person [name=null, car=Car [brand=jili, price=300000.0, tyrePerimeter=251.32741228718345], city=BeiJing, info=白领]




你可能感兴趣的:(spring)