• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Initializing list of values using Spring

May 14, 2015 By j2eereference Leave a Comment

Initializing list of values using Spring

There can be a situation where we need to initialize a list of objects while loading the class.

In this article we will see how spring supports the collection objects. Let’s take an example of Line object . Here we have one start point location ,one  end point location, and  each of these has X and Y values  eg:  start(X,Y) and  end(X,Y).

Spring has List , Map  and  Set interfaces to support for injecting the dependency of list of objects

In this example we can see how to configure the dependency of list of objects.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.j2eereference.spring;
import java.util.List;
 
public class Line {
private List points;
 
public List getPoints() {
return points;
}
public void setPoints(List points) {
this.points = points;
}
public void displayLinePoints()
{
for(Point point:points){
System.out.println("point is ("+point.getValueX()+","+point.getValueY()+")");
 
}
}
}

In the above example code we have declared the member variable points as list, which is nothing but a list of Point objects.. We have the Point class as below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.j2eereference.spring;
public class Point {
 
private int valueX;
private int valueY;
public int getValueX() {
return valueX;
}
public void setValueX(int valueX) {
this.valueX = valueX;
}
public int getValueY() {
return valueY;
}
public void setValueY(int valueY) {
this.valueY = valueY;
}
}

Now We will see how to configure initializing the list values in spring.xml

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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.xsd">
 
  <bean id="LineBean" class="com.j2eereference.spring.Line">
    <property name="points">
       <list>
         <ref bean="startPoint"/>
         <ref bean="endPoint"/>
        </list>
     </property>
  </bean>
 
  <bean id="startPoint" class="com.j2eereference.spring.Point">
    <property name="valueX" value="10"/>
    <property name="valueY" value="20"/>
  </bean>
 
  <bean id="endPoint" class="com.j2eereference.spring.Point">
    <property name="valueX" value="20"/>
    <property name="valueY" value="30"/>
  </bean>
</beans>

Let’s run the below code to print the value of list objects which are configured in the spring.xml file.

1
2
3
4
5
6
7
8
9
10
11
12
package com.j2eereference.spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
Line line= (Line)context.getBean("LineBean");
line.displayLinePoints();
}
}

output

12 May, 2015 12:50:08 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@16fd0b7: startup date [Tue May 12 00:50:08 IST 2015]; root of context hierarchy
12 May, 2015 12:50:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]

point is (10,20)
point is (20,30)

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.