阅读(4586) (0)

Spring教程 - Spring表达式语言使用

2017-01-09 19:06:19 更新

Spring教程 - Spring表达式语言使用


数据库属性文件...

bean.property_name

在下面的代码中,我们从“addressBean"注入了“country"属性的值,bean into“customer"class“country"属性。

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


例子

以下代码定义了一个Address bean,并用Spring表达式语言标记该bean。

它用字符串值填充街道,用int值填充邮政编码。 它还定义了一个实用方法 getFullAddress 返回邮政编码,街道,和国家。

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中定义的值来填充服务器bean中的属性。

在Spring Expression语言中,我们还可以从bean调用该方法。

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文件配置中填充相同的数据。

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


测试

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

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

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

实施例2

下面的代码显示了如何在Spring Expression Language中调用没有参数的方法。

首先,我们使用返回double值的方法定义一个Java bean。

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

在下面的代码中,我们调用上面在Spring表达式语言中定义的方法。

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

我们也可以在String字面量上调用“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>