Spring EL Bean Referenzbeispiel
In Spring EL können Sie mit dem Symbol 'dot (.)' auf eine Bean und verschachtelte Eigenschaften verweisen. Zum Beispiel „bean.property_name“.
public class Customer { @Value("#{addressBean.country}") private String country;
Im obigen Code-Snippet wird der Wert der Eigenschaft "country" aus der Bean "addressBean" in die aktuelle Eigenschaft "customer", "country", eingefügt.
Frühling EL in der Anmerkung
Das folgende Beispiel zeigt Ihnen, wie Sie mit SpEL auf ein Bean, eine Bean-Eigenschaft und dessen Methode verweisen.
package com.example.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{addressBean}") private Address address; @Value("#{addressBean.country}") private String country; @Value("#{addressBean.getFullAddress('example')}") private String fullAddress; //getter and setter methods @Override public String toString() { return "Customer [address=" + address + "\n, country=" + country + "\n, fullAddress=" + fullAddress + "]"; } }
package com.example.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("addressBean") public class Address { @Value("Block ABC, LakeView") private String street; @Value("98700") private int postcode; @Value("US") private String country; public String getFullAddress(String prefix) { return prefix + " : " + street + " " + postcode + " " + country; } //getter and setter methods public void setCountry(String country) { this.country = country; } @Override public String toString() { return "Address [street=" + street + ", postcode=" + postcode + ", country=" + country + "]"; } }
Starte es
Customer obj = (Customer) context.getBean("customerBean"); System.out.println(obj);
Ausgabe
Customer [address=Address [street=Block ABC, LakeView, postcode=98700, country=US] , country=US , fullAddress=example : Block ABC, LakeView 98700 US]
Spring EL in XML
Siehe äquivalente Version in der Bean-Definitions-XML-Datei.
Quellcode herunterladen
Laden Sie es herunter -Spring3-EL-Bean-Reference-Example.zip (6 KB)