Just like JavaConfig being an alternate way to provide configuration
metadata instead of xml files, Annotations can be used in place of xml
metadata in spEL as well.
File : Candidate.java
File : Address.java
File : elBeans.xml
File : MainClass.java
Step 1 : Create the POJOs, Candidate.java and Address.java
File : Candidate.java
package com.simpleCodeStuffs.spEL; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("elCandidate") public class Candidate { @Value("Poorni") private String name; @Value("27") private int age; @Value("#{elAddress}") private Address addrs; @Value("#{elAddress.area}") private String area; public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddrs() { return addrs; } public void setAddrs(Address addrs) { this.addrs = addrs; } }
File : Address.java
package com.simpleCodeStuffs.spEL; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("elAddress") public class Address { @Value("22/7") private String doorNo; @Value("MKM Street") private String street; @Value("Ambattur") private String area; public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getDoorNo() { return doorNo; } public void setDoorNo(String doorNo) { this.doorNo = doorNo; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
Step 2 : Enable auto component scanning in the xml file
File : elBeans.xml
<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.simpleCodeStuffs.spEL" /> </beans>
Step 3 : Main class
File : MainClass.java
package com.simpleCodeStuffs.spEL; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainClass { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("elBeans.xml"); Candidate can = (Candidate) context.getBean("elCandidate"); Address add = (Address) context.getBean("elAddress"); System.out.println("Name : " + can.getName()); System.out.println("Age : " + can.getAge()); System.out.println("Area : " + can.getArea()); System.out.println("Address : " + can.getAddrs().getDoorNo()); System.out.println("t " + add.getStreet()); System.out.println("t " + add.getArea()); } }