Spring教程 - Spring表達(dá)式語(yǔ)言使用

2018-01-09 19:06 更新

Spring教程 - Spring表達(dá)式語(yǔ)言使用


數(shù)據(jù)庫(kù)屬性文件...

bean.property_name

在下面的代碼中,我們從“addressBean"注入了“country"屬性的值,bean into“customer"class“country"屬性。

public class Server {
  @Value("#{addressBean.country}")
  private String country;
    ...
}


例子

以下代碼定義了一個(gè)Address bean,并用Spring表達(dá)式語(yǔ)言標(biāo)記該bean。

它用字符串值填充街道,用int值填充郵政編碼。 它還定義了一個(gè)實(shí)用方法 getFullAddress 返回郵政編碼,街道,和國(guó)家。

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("addressBean")
public class Address {

    @Value("Main Street, New York")
    private String street;

    @Value("123456")
    private int postcode;

    @Value("US")
    private String country;

    public String getFullAddress(String prefix) {
        return prefix + " : " + street + " " + postcode + " " + country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    @Override
    public String toString() {
        return "Address [street=" + street + ", postcode=" + postcode
                + ", country=" + country + "]";
    }
    public String getStreet() {
      return street;
    }
    public void setStreet(String street) {
      this.street = street;
    }
    public int getPostcode() {
      return postcode;
    }
    public void setPostcode(int postcode) {
      this.postcode = postcode;
    }
    public String getCountry() {
      return country;
    }
}

以下代碼使用Address Java bean中定義的值來(lái)填充服務(wù)器bean中的屬性。

在Spring Expression語(yǔ)言中,我們還可以從bean調(diào)用該方法。

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("myServer")
public class Server {
    @Value("#{addressBean}")
    private Address address;
    @Value("#{addressBean.country}")
    private String country;
    @Value("#{addressBean.getFullAddress("java2s")}")
    private String fullAddress;
    @Override
    public String toString() {
        return "Server [address=" + address + "\n, country=" + country
                + "\n, fullAddress=" + fullAddress + "]";
    }
    public Address getAddress() {
      return address;
    }
    public void setAddress(Address address) {
      this.address = address;
    }
    public String getCountry() {
      return country;
    }
    public void setCountry(String country) {
      this.country = country;
    }
    public String getFullAddress() {
      return fullAddress;
    }
    public void setFullAddress(String fullAddress) {
      this.fullAddress = fullAddress;
    }
}

以下代碼顯示如何在xml文件配置中填充相同的數(shù)據(jù)。

<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="myServer" class="com.java2s.core.Server">
    <property name="address" value="#{addressBean}" />
    <property name="country" value="#{addressBean.country}" />
    <property name="fullAddress" value="#{addressBean.getFullAddress("java2s")}" />
  </bean>
  <bean id="addressBean" class="com.java2s.core.Address">
    <property name="street" value="Main Street, New York" />
    <property name="postcode" value="123456" />
    <property name="country" value="US" />
  </bean>
</beans>


測(cè)試

<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="myServer" class="com.java2s.core.Server">
  </bean>
</beans>

以下代碼顯示了如何運(yùn)行上面的代碼。

package com.java2s.core;
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);
  }
}

實(shí)施例2

下面的代碼顯示了如何在Spring Expression Language中調(diào)用沒有參數(shù)的方法。

首先,我們使用返回double值的方法定義一個(gè)Java bean。

package com.java2s.core;
import org.springframework.stereotype.Component;
@Component("priceBean")
public class Price {
  public double getSpecialPrice() {
    return new Double(99.99);
  }
}

在下面的代碼中,我們調(diào)用上面在Spring表達(dá)式語(yǔ)言中定義的方法。

  @Value("#{priceBean.getSpecialPrice()}")
  private double amount;

我們也可以在String字面量上調(diào)用“toUpperCase()"方法。

    @Value("#{"java2s".toUpperCase()}")
  private String name;

完整的源代碼

package com.java2s.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myServer")
public class Server {

  @Value("#{"java2s".toUpperCase()}")
  private String name;

  @Value("#{priceBean.getSpecialPrice()}")
  private double amount;

  public String getName() {
    return name;
  }

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

  public double getAmount() {
    return amount;
  }

  public void setAmount(double amount) {
    this.amount = amount;
  }

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

}

下面的代碼顯示了如何在bean定義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="myServer" class="com.java2s.core.Server">
    <property name="name" value="#{"java2s".toUpperCase()}" />
    <property name="amount" value="#{priceBean.getSpecialPrice()}" />
  </bean>

  <bean id="priceBean" class="com.java2s.core.Price" />

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)