举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > spring 表达式语言 Spring教程 - Spring表达式语言

spring 表达式语言 Spring教程 - Spring表达式语言

2023-03-18 03:20 Spring教程

spring 表达式语言 Spring教程 -  Spring表达式语言

spring 表达式语言

Spring表达式语言(SpEL)是一种强大的表达式语言,它可以用来在运行时创建、配置和操作对象。它可以用来检索和操作对象的属性,执行方法调用,创建新对象,使用内置函数或者使用任何其他的 Java 表达式语法。

Spring表达式语言是一个功能强大的表达式语言,它可以在运行时创建、配置和操作对象。它可以用来检索和操作对象的属性,执行方法调用,创建新对象,使用内置函数或者使用任何其他的 Java 表达式语法。

String expression = "T(java.lang.Math).random() * 100.0"; 
double value = parser.parseExpression(expression).getValue(Double.class); 
System.out.println("Random value is : " + value); 

Spring SpEL 支持多种数字标量类型(如 int、long 和 double 等)、字符串、布尔值、null 和正则表达式。此外,它还支持集合、数组、Map 和其他 JavaBean 的特性。SpEL 还可以通过使用内部函数来扩充其功能。例如:

 
String expression = "T(java.lang.Math).abs(-100)"; 
int value = parser.parseExpression(expression).getValue(Integer.class); 
System.out.println("Absolute value is : " + value); 
 

Spring SpEL 支持多个运算符来扩充其功能。例如“+”、“-”、“*”、“/” 等常用运算符都可以在 Spring SpEL 中使用。此外 Spring SpEL 还有一些特有的运算符如 “? : ” (三目运算符) 和 “#this ? .methodName() : null ” (Elvis 运算符 ) 等。

Spring教程 - Spring表达式语言

Spring教程 - Spring表达式语言


Spring 3.0引入了一个强大的表达式语言称为Spring表达式语言,或Spring EL。

Spring表达式语言,通过XML或注释,在bean创建时间期间被评估或执行。

春天表达语言hello世界

在下面的代码中展示了如何使用Spring表达式语言注入String,整数和bean到属性,在XML和注释。

为了使用Spring表达式语言,我们需要添加以下jar  对pom.xml文件的依赖。

...
    <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
  <dependencies>
...


Java Bean

下面的代码定义了两个Java bean,后面我们将使用Spring Expression Language在XML和注释中将值注入属性。

服务器Java Bean。

package com.www..cnmon;

public class Server {

  private Item item;

  private String itemName;

  public Item getItem() {
    return item;
  }

  public void setItem(Item item) {
    this.item = item;
  }

  public String getItemName() {
    return itemName;
  }

  public void setItemName(String itemName) {
    this.itemName = itemName;
  }

  @Override
  public String toString() {
    return "Server [item=" + item + ", itemName=" + itemName + "]";
  }

}

项目Java Bean。

package com.www..cnmon;


public class Item {

  private String name;

  private int qty;
  public String getName() {
    return name;
  }
  @Override
  public String toString() {
    return "Item [name=" + name + ", qty=" + qty + "]";
  }
  public int getQty() {
    return qty;
  }
  public void setQty(int qty) {
    this.qty = qty;
  }
  public void setName(String name) {
    this.name = name;
  }
}


XML中的Spring EL

下面的代码显示了如何在XML中使用Spring EL。

下面的代码显示了如何在XML中使用Spring EL。...

  <bean id="itemBean" class="com.www..cnmon.Item">
    <property name="name" value="itemA" />
    <property name="qty" value="10" />
  </bean>

然后,我们通过重用Item Bean中的值来创建XML中的服务器bean。

Spring表达式语言用#{expression}括起来。以下代码引用Item bean的值。它将itemBean分配给item,将itemBean.name分配给itemName。 itemBean.name 的值为itemA。

  <bean id="myServer" class="com.www..cnmon.Server">
    <property name="item" value="#{itemBean}" />
    <property name="itemName" value="#{itemBean.name}" />
  </bean>

完整配置文件。

<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-3.0.xsd">
  <bean id="itemBean" class="com.www..cnmon.Item">
    <property name="name" value="itemA" />
    <property name="qty" value="10" />
  </bean>
  <bean id="myServer" class="com.www..cnmon.Server">
    <property name="item" value="#{itemBean}" />
    <property name="itemName" value="#{itemBean.name}" />
  </bean>
</beans>

Download Java2s_Spring_EL_XML.zip

Spring EL的注释

以下示例显示如何在注释中使用Spring表达式语言。

首先,我们定义一个Java Bean项目,并用 Component 注释标记它。对于其属性,我们使用 Value 注释来分配它们的值

package com.www..cnmon;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item {
  @Value("itemA") //inject String directly
  private String name;
  @Value("10") //inject interger directly
  private int qty;
  public String getName() {
    return name;
  }
  @Override
  public String toString() {
    return "Item [name=" + name + ", qty=" + qty + "]";
  }
  public int getQty() {
    return qty;
  }
  public void setQty(int qty) {
    this.qty = qty;
  }
  public void setName(String name) {
    this.name = name;
  }
}

然后,我们定义一个服务器Java bean,并用 Component 注释来标记它。当定义Server的属性时,我们使用从Item bean定义的 Value 注释的值。

package com.www..cnmon;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myServer")
public class Server {

  @Value("#{itemBean}")
  private Item item;

  @Value("#{itemBean.name}")
  private String itemName;

  public Item getItem() {
    return item;
  }

  public void setItem(Item item) {
    this.item = item;
  }

  public String getItemName() {
    return itemName;
  }

  public void setItemName(String itemName) {
    this.itemName = itemName;
  }

  @Override
  public String toString() {
    return "Server [item=" + item + ", itemName=" + itemName + "]";
  }

}

最后,我们必须在xml文件中启用自动组件扫描。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.www..cnmon" />

</beans>

例子

以下代码显示了如何运行上面的代码。

package com.www..cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
  public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Server obj = (Server) context.getBean("myServer");
      System.out.println(obj);
  }
}

上面的代码生成以下结果。


Download Java2s_spring_EL_Annotation.zip

Spring表达式语言中的集合

以下代码显示了如何在Spring表达式语言中使用集合。

首先,定义一个包含集合的Java bean。

package com.www..cnmon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component("testBean")
public class Test {

  private Map<String, String> map;
  private List<String> list;

  public Test() {
    map = new HashMap<String, String>();
    map.put("MapA", "This is A");
    map.put("MapB", "This is B");
    map.put("MapC", "This is C");

    list = new ArrayList<String>();
    list.add("List0");
    list.add("List1");
    list.add("List2");
  }
}

然后,使用表达式语言中的集合。

package com.www..cnmon;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

  @Value("#{testBean.map["MapA"]}")
  private String mapA;

  @Value("#{testBean.list[0]}")
  private String list;

}

在xml配置文件中使用相同的设置。

<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-3.0.xsd">

  <bean id="customerBean" class="com.www..cnmon.Customer">
    <property name="mapA" value="#{testBean.map["MapA"]}" />
    <property name="list" value="#{testBean.list[0]}" />
  </bean>

  <bean id="testBean" class="com.www..cnmon.Test" />

</beans>
阅读全文
以上是名动网为你收集整理的spring 表达式语言 Spring教程 - Spring表达式语言全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 名动网 mdwl.vip 版权所有 联系我们