Spring教程 - Spring表達式語言

2018-01-09 19:06 更新

Spring教程 - Spring表達式語言


Spring 3.0引入了一個強大的表達式語言稱為Spring表達式語言,或Spring EL。

Spring表達式語言,通過XML或注釋,在bean創(chuàng)建時間期間被評估或執(zhí)行。

春天表達語言hello世界

在下面的代碼中展示了如何使用Spring表達式語言注入String,整數(shù)和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.o2fo.common;

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.o2fo.common;


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.o2fo.common.Item">
    <property name="name" value="itemA" />
    <property name="qty" value="10" />
  </bean>

然后,我們通過重用Item Bean中的值來創(chuàng)建XML中的服務器bean。

Spring表達式語言用#{expression}括起來。以下代碼引用Item bean的值。它將itemBean分配給item,將itemBean.name分配給itemName。 itemBean.name 的值為itemA。

  <bean id="myServer" class="com.www.o2fo.common.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.o2fo.common.Item">
    <property name="name" value="itemA" />
    <property name="qty" value="10" />
  </bean>
  <bean id="myServer" class="com.www.o2fo.common.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.o2fo.common;

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.o2fo.common;
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.o2fo.common" />

</beans>

例子

以下代碼顯示了如何運行上面的代碼。

package com.www.o2fo.common;
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.o2fo.common;

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.o2fo.common;

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.o2fo.common.Customer">
    <property name="mapA" value="#{testBean.map["MapA"]}" />
    <property name="list" value="#{testBean.list[0]}" />
  </bean>

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

</beans>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號