How to configure List in Spring Bean
We can configure List in Spring bean using <list> tag within <property> tag. We can configure list literals and object reference.
Let's configure list of String and Address below Employee class-
Run It:
Let's configure list of String and Address below Employee class-
package com.javagladiator.spring.core.trainning;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Employee {
private Integer empId;
private String name;
private List<String> phones;
private List<Address> addresses;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPhones() {
return phones;
}
public void setPhones(List<String> phones) {
this.phones = phones;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name="
+ name + ", phones=" + phones
+ ", addresses=" + addresses + "]";
}
}
Address class:package com.javagladiator.spring.core.trainning;
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [city=" + city + "]";
}
}
application.xml Configures:<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="employee"
class="com.javagladiator.spring.core.trainning.Employee">
<property name="empId" value="1001" />
<property name="name" value="Parthiv Singh" />
<property name="phones">
<list>
<value>9898989</value>
<value>2323455</value>
<value>4534343</value>
</list>
</property>
<property name="addresses">
<list>
<ref bean="currentAddress"/>
<ref bean="permanentAddress"/>
<ref bean="officeAddress"/>
</list>
</property>
</bean>
<bean id="currentAddress"
class="com.javagladiator.spring.core.trainning.Address">
<property name="city" value="Pune"/>
</bean>
<bean id="permanentAddress"
class="com.javagladiator.spring.core.trainning.Address">
<property name="city" value="Jodhpur"/>
</bean>
<bean id="officeAddress"
class="com.javagladiator.spring.core.trainning.Address">
<property name="city" value="Mumbai"/>
</bean>
</beans>
Run It:
package com.javagladiator.spring.core.trainning;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.*;
public class App
{
public static void main( String[] args ){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("application.xml");
Employee employee =
(Employee) applicationContext.getBean("employee");
System.out.println(employee);
}
}
Comments
Post a Comment