Pages Navigation Menu

Coding is much easier than you think

Dot operator on Bean reference

 

We have seen previously that, to access an attribute ‘attr’ of a bean ‘beanA’ we can use ‘beanA.attr’. Here, we make use of the ‘.’ (dot operator) to access the properties of a bean. Similarly the dot operator can be made use of to further reference nested properties of the attribute.

Let us take that, inside the POJO Candidate, I need to store details of the number of characters in a property(street) of the inner bean Address. This can be done just as simply by using the ‘.’ Operator in spEL.

Any such operation on an existing property from the inner bean can be obtained by the outer bean by a similar procedure.
 

Step 1 : Create the POJO

 

Candidate.java

package com.simpleCodeStuffs.spEL;

public class Candidate {
private String name;
private int age;
private Address addrs;
private String area;
private int streetLength;
public int getStreetLength() {
return streetLength;
}
public void setStreetLength(int streetLength) {
this.streetLength = streetLength;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public Candidate(){
this.name=null;
this.age=0;
this.addrs=null;
}
public Candidate(String name,int age,Address addrs){
this.name=name;
this.age=age;
this.addrs=addrs;
}
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;
}

}

 

Address.java
 

package com.simpleCodeStuffs.spEL;

public class Address {
private String doorNo;
private String street;
private String area;
public Address(){
this.doorNo=null;
this.street=null;
this.area=null;
}
public Address(String doorNo, String street,String area){
this.doorNo=doorNo;
this.street=street;
this.area=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 :

Main Class – No change here. Simply print the newly added attribute streetLength from Candidate class.
 

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());
System.out.println("StreetLength"+can.getStreetLength());
}
}

Step 3 :

Modification in the elBeans.xml to get the property length of the inner bean
We have seen that the street property inside Address bean can be referred as #{Address.street} , now further the length of the same can be assigned in injection by doing a #{Address.street.length()}

elBeans.xml



	
		
		
		
	

	
		
		
		
		
		
	

 

Step 4 : Run the program. The output is

 

spelDotOutput