Spring expression language basic example using annotations.
Spring 3 introduced the Spring Expression Language (SpEL), a powerful expression language, used to wire values into bean’s properties.
It’s an expression language similar to other ELs, that supports querying and manipulating an object graph at runtime.
This page gives introduction to spring expression language with basic example using annotations, you can see more examples
in the next pages. Previous page has given example using xml based configuration. Here is the spring beans.
You must register your beans using @Component, and specify your expression language using @Value annotation:
In the below bean class, the expression language #{beanOrder} assigns "beanOrder" reference to order property, and
#{beanOrder.item} assigns beanOrder item name to item property.
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("beanOrder")
public class Order {
@Value("Java Book")
private String item;
@Value("Rs 22.50")
private String price;
@Value("India")
private String address;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
|
package com.java2novice.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("paymentGateway")
public class PaymentGateway {
@Value("#{beanOrder.item}")
private String itemName;
@Value("#{beanOrder}")
private Order order;
public void setOrder(Order ord){
this.order = ord;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Order getOrder() {
return order;
}
public void processOrder(){
System.out.println("Processing order: "+order.getItem());
}
}
|
Here is the xml based configuration, you must enable component scan.
<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.java2novice.beans" />
</beans>
|
Spring bean demo class:
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.PaymentGateway;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway gateway = (PaymentGateway) context.getBean("paymentGateway");
gateway.processOrder();
}
}
|
|