Spring --- SpEL

 一)什么是SpEL
SpEL -- Spring Expression Language. Spring的表达式语言。举个最简单的例子:

Java代码 复制代码 收藏代码
  1. ExpressionParser parser =new SpelExpressionParser();
  2. Expression exp = parser.parseExpression("'Hello World'");
  3. String message = (String) exp.getValue();
ExpressionParser parser =new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();


最后 message的值就是 Hello World, 表达式中的单引号''就是表达String类型的一种格式。另外值得注意的一点时,当表达式不符合规范时, parser.parseExpression语句会抛出ParseException;而exp.getValue会抛出EvaluationException
而为了避免最后的类型转换,我们还可以这样写:

Java代码 复制代码 收藏代码
  1. String message = exp.getValue(String.class);
String message = exp.getValue(String.class);



二)SpEL举例中需要使用到的类
以下是本文介绍SpEL过程中需要使用到的类,在此列出,以供后续介绍中使用参考:
Inventor.java

Java代码 复制代码 收藏代码
  1. package org.spring.samples.spel.inventor;
  2. import java.util.Date;
  3. import java.util.GregorianCalendar;
  4.  
  5. public class Inventor {
  6. private String name;
  7. private String nationality;
  8. private String[] inventions;
  9. private Date birthdate;
  10. private PlaceOfBirth placeOfBirth;
  11.  
  12. public Inventor(String name, String nationality){
  13. GregorianCalendar c= new GregorianCalendar();
  14. this.name = name;
  15. this.nationality = nationality;
  16. this.birthdate = c.getTime();
  17. }
  18. public Inventor(String name, Date birthdate, String nationality) {
  19. this.name = name;
  20. this.nationality = nationality;
  21. this.birthdate = birthdate;
  22. }
  23. public Inventor() {}
  24.  
  25. public String getName() { return name;}
  26. public void setName(String name) { this.name = name;}
  27.  
  28. public String getNationality() { return nationality;}
  29. public void setNationality(String nationality) { this.nationality = nationality; }
  30.  
  31. public Date getBirthdate() { return birthdate;}
  32. public void setBirthdate(Date birthdate) { this.birthdate = birthdate;}
  33.  
  34. public PlaceOfBirth getPlaceOfBirth() { return placeOfBirth;}
  35. public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) { this.placeOfBirth = placeOfBirth;}
  36.  
  37. public void setInventions(String[] inventions) { this.inventions = inventions; }
  38. public String[] getInventions() { return inventions;}
  39. }
package org.spring.samples.spel.inventor;
import java.util.Date;
import java.util.GregorianCalendar;

public class Inventor {
  private String name;
  private String nationality;
  private String[] inventions;
  private Date birthdate;
  private PlaceOfBirth placeOfBirth;

  public Inventor(String name, String nationality){
    GregorianCalendar c= new GregorianCalendar();
    this.name = name;
    this.nationality = nationality;
    this.birthdate = c.getTime();
  }
  public Inventor(String name, Date birthdate, String nationality) {
    this.name = name;
    this.nationality = nationality;
    this.birthdate = birthdate;
  }
  public Inventor() {}

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

  public String getNationality() { return nationality;}
  public void setNationality(String nationality) { this.nationality = nationality; }

  public Date getBirthdate() { return birthdate;}
  public void setBirthdate(Date birthdate) { this.birthdate = birthdate;}

  public PlaceOfBirth getPlaceOfBirth() { return placeOfBirth;}
  public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) { this.placeOfBirth = placeOfBirth;}

  public void setInventions(String[] inventions) { this.inventions = inventions; }
  public String[] getInventions() { return inventions;}
}


PlaceOfBirth.java

Java代码 复制代码 收藏代码
  1. package org.spring.samples.spel.inventor;
  2.  
  3. public class PlaceOfBirth {
  4. private String city;
  5. private String country;
  6.  
  7. public PlaceOfBirth(String city) {this.city=city;}
  8. public PlaceOfBirth(String city, String country){
  9. this(city);
  10. this.country = country;
  11. }
  12.  
  13. public String getCity() {return city;}
  14. public void setCity(String s) {this.city = s;}
  15.  
  16. public String getCountry() {return country;}
  17. public void setCountry(String country) {this.country = country;}
  18. }
package org.spring.samples.spel.inventor;

public class PlaceOfBirth {
  private String city;
  private String country;

  public PlaceOfBirth(String city) {this.city=city;}
  public PlaceOfBirth(String city, String country){
    this(city);
    this.country = country;
  }

  public String getCity() {return city;}
  public void setCity(String s) {this.city = s;}

  public String getCountry() {return country;}
  public void setCountry(String country) {this.country = country;}
}


Society.java

Java代码 复制代码 收藏代码
  1. package org.spring.samples.spel.inventor;
  2. import java.util.*;
  3.  
  4. public class Society {
  5. private String name;
  6. public static String Advisors = "advisors";
  7. public static String President = "president";
  8. private List<Inventor> members = new ArrayList<Inventor>();
  9. private Map officers = new HashMap();
  10.  
  11. public List getMembers() {return members;}
  12. public Map getOfficers() {return officers;}
  13. public String getName() {return name;}
  14. public void setName(String name) {this.name = name;}
  15. public boolean isMember(String name){
  16. boolean found = false;
  17. for (Inventor inventor : members) {
  18. if (inventor.getName().equals(name)){
  19. found = true;
  20. break;
  21. }
  22. }
  23. return found;
  24. }
  25. }
package org.spring.samples.spel.inventor;
import java.util.*;

public class Society {
  private String name;
  public static String Advisors = "advisors";
  public static String President = "president";
  private List<Inventor> members = new ArrayList<Inventor>();
  private Map officers = new HashMap();

  public List getMembers() {return members;}
  public Map getOfficers() {return officers;}
  public String getName() {return name;}
  public void setName(String name) {this.name = name;}
  public boolean isMember(String name){
    boolean found = false;
    for (Inventor inventor : members) {
      if (inventor.getName().equals(name)){
        found = true;
        break;
      }
    }
    return found;
  }
}

 

你可能感兴趣的:(S)