Advertisement
package decodejava;
import java.util.Map;
public class Company
{
public String name;
public Map<Integer,Employee> employees;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Map<Integer,Employee> getEmployees()
{
return employees;
}
public void setEmployees(Map<Integer,Employee> employees)
{
this.employees = employees;
}
}
package decodejava;
public class Employee
{
public String name;
public int telNo;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getTelNo()
{
return telNo;
}
public void setTelNo(int telNo)
{
this.telNo = telNo;
}
}
Advertisement
package decodejava;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Utility {
public static void main(String[] args)
{
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.beans.xml");
Company company = context.getBean(Company.class);
StandardEvaluationContext stContext = new StandardEvaluationContext(company);
SpelExpressionParser parser = new SpelExpressionParser();
Map<Integer,Employee> map = (Map<Integer,Employee>)parser.parseRaw("employees.?[key == 2]").getValue(stContext);
Set<Map.Entry<Integer,Employee>> set = map.entrySet();
for(Map.Entry<Integer,Employee> employee : set)
{
System.out.println("Employee ID : " + employee.getKey());
System.out.println("Employee Name : " + employee.getValue().getName());
System.out.println("Employee Tel No : " + employee.getValue().getTelNo());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/context/spring-context.xsd
">
<bean id="CompanyBean" class="decodejava.Company">
<property name="employees">
<map>
<entry key="1" value-ref="Employee1"></entry>
<entry key="2" value-ref="Employee2"></entry>
<entry key="3" value-ref="Employee3"></entry>
</map>
</property>
</bean>
<bean class="decodejava.Employee" id="Employee1">
<property name="name" value="Emp1"></property>
<property name="telNo" value="111111"></property>
</bean>
<bean class="decodejava.Employee" id="Employee2">
<property name="name" value="Emp2"></property>
<property name="telNo" value="222222"></property>
</bean>
<bean class="decodejava.Employee" id="Employee3">
<property name="name" value="Emp3"></property>
<property name="telNo" value="333333"></property>
</bean>
</beans>
Advertisement
Aug 21, 2018 5:35:42 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@626b2d4a: startup date [Tue Aug 21 17:35:42 2018]; root of context hierarchy
Aug 21, 2018 5:35:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [config.beans.xml]
Employee ID : 2
Employee Name : Emp2
Employee Tel No : 222222
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement