zoukankan      html  css  js  c++  java
  • Spring框架:第五章:Spring EL表达式

    SpEL简介
    Spring Expression Language,Spring表达式语言,简称SpEL。支持运行时查询并可以操作对象图。
    和JSP页面上的EL表达式、Struts2中用到的OGNL表达式一样,SpEL根据JavaBean风格的getXxx()、setXxx()方法定义的属性访问对象图,完全符合我们熟悉的操作习惯。

    基本语法
    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}"/>
    

    引用其他bean

    <bean id="emp04" class="com.parent.bean.Employee">
    	<property name="empId" value="1003"/>
    	<property name="empName" value="Kate"/>
    	<property name="age" value="21"/>
    	<property name="detp" value="#{dept}"/>
    </bean>
    

    引用其他bean的属性值作为自己某个属性的值

    <bean id="emp05" class="com.parent.bean.Employee">
    	<property name="empId" value="1003"/>
    	<property name="empName" value="Kate"/>
    	<property name="age" value="21"/>
    	<property name="deptName" value="#{dept.deptName}"/>
    </bean>
    

    调用非静态方法

    <!-- 创建一个对象,在SpEL表达式中调用这个对象的方法 -->
    <bean id="salaryGenerator" class="com.spel.bean.SalaryGenerator"/>
    
    <bean id="employee" class="com.spel.bean.Employee">
    	<!-- 通过对象方法的返回值为属性赋值 -->
    	<property name="salayOfYear" value="#{salaryGenerator.getSalaryOfYear(5000)}"/>
    </bean>
    

    调用静态方法

    <bean id="employee" class="com.spel.bean.Employee">
    	<!-- 在SpEL表达式中调用类的静态方法 -->
    	<property name="circle" value="#{T(java.lang.Math).PI*20}"/>
    </bean>
    

    运算符
    ①算术运算符:+、-、*、/、%、^
    ②字符串连接:+
    ③比较运算符:<、>、==、<=、>=、lt、gt、eq、le、ge
    ④逻辑运算符:and, or, not, |
    ⑤三目运算符:判断条件?判断结果为true时的取值:判断结果为false时的取值
    ⑥正则表达式:matches

    创建java实体Bean对象

    public class Person {
    
    	private int id;
    	private String name;
    	private String phone;
    	private double salary;
    	private Car car;
    
    
    public class Car {
    
    	private String name;
    	private String carNo;
    

    实验26:[SpEL测试I]在SpEL中使用字面量

    使用格式:#{数值} #{“字符串” || ‘字符串’}

    实验27:[SpEL测试II]在SpEL中引用其他bean

    使用格式:#{bean的id}

    实验28:[SpEL测试III]在SpEL中引用其他bean的某个属性值

    使用格式: #{bean.属性名}

    实验29:[SpEL测试IV]在SpEL中调用非静态方法

    使用格式: #{bean.方法名(参数)}

    实验30:[SpEL测试V]在SpEL中调用静态方法

    使用格式:#{T(全名类).方法名(参数)}

    实验31:[SpEL测试VI]在SpEL中使用运算符

    使用格式:#{表达式}

    <?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="car" class="com.atguigu.pojo.Car">
    		<property name="carNo" value="京B143122" />
    		<property name="name" value="蓝波基泥" />
    	</bean>
    
    	
    	<!-- 实验26:[SpEL测试I]在SpEL中使用字面量 -->
    	<!-- 使用格式:#{数值} 		#{“字符串” || ‘字符串’} -->
    	<bean id="p1" class="com.atguigu.pojo.Person">
    		<property name="id" value="#{1}" />
    		<!-- 		<property name="name" value="#{'这是el的字符串'}" /> -->
    		<!-- 实验27:[SpEL测试II]在SpEL中引用其他bean -->
    		<!-- 使用格式:#{bean的id} -->
    		<property name="car" value="#{car}" />
    		<!-- 实验28:[SpEL测试III]在SpEL中引用其他bean的某个属性值 -->
    		<!-- 使用格式: #{bean.属性名} -->
    <!-- 		<property name="name" value="#{car.name}"/> -->
    		<!-- 实验29:[SpEL测试IV]在SpEL中调用非静态方法 -->
    		<!-- 使用格式: #{bean.方法名(参数)} -->		
    <!-- 		<property name="name" value="#{car.fun()}" /> -->
    		<!-- 实验30:[SpEL测试V]在SpEL中调用静态方法 -->
    		<!-- 使用格式:#{T(全名类).方法名(参数)} -->
    		<property name="name" value="#{T(com.atguigu.pojo.Car).fun2()}" />
    		<!-- 实验31:[SpEL测试VI]在SpEL中使用运算符 -->
    		<!-- 使用格式:#{表达式} -->
    		<property name="salary" value="#{10000*20}" />
    	</bean>
    
    </beans>
    

    测试类:

    public class SpringTest {
    
    	@Test
    	public void test1() throws Exception {
    		
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    		
    		Person person = (Person) applicationContext.getBean("p1");
    		
    		System.out.println( person );
    	}
    	
    }
    
  • 相关阅读:
    Mybatis基础配置及增删查改操作
    SpringMVC注解方式与文件上传
    SpringMVC的基础配置及视图定位
    Spring AOP面向切面编程
    Spring注入属性、对象
    Spring的配置及jar包下载
    多线程
    集合框架
    I/O————流
    I/O————对象流
  • 原文地址:https://www.cnblogs.com/javawxid/p/12812081.html
Copyright © 2011-2022 走看看