Spring Expression (SpEL) Ternary and Elvis Operator with Example
6272 Views
Hints
Below is an example of "Spring Expression (SpEL) Ternary and Elvis Operator with Example"
Step.1 Start a Java Project with required jars
Open Eclipse
Click on menu New -> Others
In wizards type "Java Project" and Select "Java Project"
Click Next
Enter project name as "TernaryOperatorExample", then click Next
Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
Click Finish.
Step.2 Project Explorer Preview
RunMyProgram.java
package com.springexample;
import java.util.Iterator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RunMyProgram {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserNameHolder userNameDetails = (UserNameHolder) context.getBean("userNameHolder");
System.out.println("Usernames");
for (Iterator nameIterator = userNameDetails.getUserNames().iterator();nameIterator.hasNext();){
String name = nameIterator.next();
System.out.println(name);
}
}
}
UserNameHolder.java
package com.springexample;
import java.util.ArrayList;
import java.util.List;
public class UserNameHolder {
List userNames = new ArrayList();
public List getUserNames() {
return userNames;
}
public void setUserNames(List userNames) {
this.userNames = userNames;
}
}
UserNameDetails.java
package com.springexample;
public class UserNameDetails {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}