DAOService.java

public class DAOService 
{
	private List<Person> arrPersons = new ArrayList<Person>(); 
			
	public DAOService()
	{
		
		Person objPerson = new Person();
		objPerson.setName("Person 1");
		objPerson.setLocation("Teynampet");
		
		arrPersons.add(objPerson);
		
		Person objPerson2 = new Person();
		objPerson2.setName("Person 2");
		objPerson2.setLocation("TNagar");
		
		arrPersons.add(objPerson2);
	}

	public List getList()
	{
		return this.arrPersons;
	}
}

ListPerson.java

@Controller
@RequestMapping("/List")
public class ListPerson 
{	
	@Autowired
	private DAOService objDAOService;
	
	@RequestMapping("/PersonList")
	public String listPersons(Model model)
	{
		DAOService objDAOService = new DAOService();
		
		model.addAttribute("personList", this.objDAOService.getList());
		
		return "display_list";
	}
}

display_list.jsp

<title>Insert title here</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</head>
<body>	
	<table>
		<c:forEach items="${personList}" var="objPerson"> 
			<tr>
				<td>${objPerson.name}</td>
				<td>${objPerson.location}</td>
			</tr>
		</c:forEach>
	</table>
</body>

applicationContext.xml

<bean name="DAOService" class="com.mugil.controls.DAOService"></bean>

pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Method Arguments in Controller

@Controller
@RequestMapping("/Control1")
public class TestController 
{	
	@RequestMapping("/Control2")		
	public String TestMe(HttpSession session, HttpServletRequest request)
	{
		request.setAttribute("name", "Mugil Vannan");
		session.setAttribute("Test", "TestValue");
		System.out.println("1");
		return "hello";
	}
	
	@RequestMapping(value="/Control2", method=RequestMethod.POST)
	public String TestMe2(HttpSession session, HttpServletRequest request)
	{
		System.out.println(session.getAttribute("Test"));
		System.out.println(request.getParameter("cboArea"));		
		return "display";
	}
}

Alternate of HttpServletRequest request

@RequestMapping(value="/Control2", method=RequestMethod.POST)
public String TestMe2(HttpSession session, @RequestParam("cboArea") String Area)
{
  System.out.println(session.getAttribute("Test"));
  System.out.println(Area);		
  return "display";
}

Java Specification Request JSR 250

spring.xml

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd">	
	<bean id="pointC" class="com.mugil.shapes.Point">
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean> 	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="constant.properties"></property>
	</bean>	
	<bean class="com.mugil.shapes.Circle" id="circleId">
	</bean>	
	<context:annotation-config/>
</beans>

circle.java

public class Circle implements Shape{		
	private Point center;
	
	@Override
	public void drawShape() {
		System.out.println("Shape of Circle ");
		System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY());
	}

	public Point getCenter() {
		return center;
	}

	@Resource(name="pointC")
	public void setCenter(Point center) {
		this.center = center;
	}
	
	@PostConstruct
	public void initilizeMe() 
	{
		System.out.println("Initialized");
	}
	
	@PreDestroy
	public void destroyMe() 
	{
		System.out.println("Derstroyed");
	}
}

If name is not specified after the @Resource it will look for same name matching bean in the spring.xml file.

@PostConstruct and @PreDestroy will be called when circle bean is initialized and destroyed

Defining Bean using Annotations
spring.xml

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.mugil.shapes"/>
</beans>

circle.java

@Component
public class Circle implements Shape
{	
.
.
}

drawingApp.java

public class DrawingApp {
	public static void main(String[] args)  {
		 AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
		 objContext.registerShutdownHook();
		Shape objShape =  (Shape)objContext.getBean("circle");
.
.
}

Using MessageSource
spring.xml

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>constant</value>
			</list>
		</property>
	</bean>

constant.properties

TestMessage=Mugil

DrawingApp.java

 AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
System.out.println(objContext.getMessage("TestMessage", null, "Default Message", null));

Output

 Mugil

Using instance of Message Source in Bean Class
spring.xml

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>constant</value>
			</list>
		</property>
	</bean>
	<context:annotation-config/>
	<context:component-scan base-package="com.mugil.shapes"/>
</beans>

circle.java

@Component
public class Circle implements Shape{		
	private Point center;
	
	@Autowired
	private MessageSource messageSource;	
	
	@Override
	public void drawShape() {
		System.out.println("Shape of Circle ");		
		System.out.println(this.messageSource.getMessage("TestMessage", null, "Vannan", null));
	}

	public Point getCenter() {
		return center;
	}

	@Resource(name="pointC")
	public void setCenter(Point center) {
		this.center = center;
	}
	
	public MessageSource getMessageSource() {
		return messageSource;
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
}

this.messageSource.getMessage(“TestMessage”, null, “Vannan”, null)

Variable substitution in Message Source

circle.java

@Component
public class Circle implements Shape{		
	private Point center;
	
	@Autowired
	private MessageSource messageSource;
	
	
	@Override
	public void drawShape() {
		System.out.println("Center of Cirlce");		System.out.println(this.messageSource.getMessage("TestMessage2", new Object[]{center.getX(), center.getY()}, "Vannan", null));
	}
.
.
.
}

this.messageSource.getMessage(“TestMessage2”, new Object[]{center.getX(), center.getY()}, “Vannan”, null)

constant.properties

TestMessage=Mugil
TestMessage2=Center of Circle : ({0}, {1})

@Required
Incase the bean value is not defined using required will display the spring error message instead of null pointer exception for the bean whose value is not defined.

spring.xml

<bean class="com.mugil.shapes.Circle" id="circleId">		
</bean>

Note above the pointA is undefined for above circle bean

Spring Error without @Required annotation

Exception in thread "main" java.lang.NullPointerException
	at com.mugil.shapes.Circle.drawShape(Circle.java:11)
	at com.mugil.shapes.DrawingApp.main(DrawingApp.java:17)

circle.java

public class Circle implements Shape{
	private Point center;
	
	@Override
	public void drawShape() {
		System.out.println("Shape of Circle ");
		System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY());
	}

	public Point getCenter() {
		return center;
	}

	@Required
	public void setCenter(Point center) {
		this.center = center;
	}		
}

spring.xml

<bean class="com.mugil.shapes.Circle" id="circleId">		
</bean>
	
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

Spring Error is Thrown

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleId' defined in class path resource [spring1.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'center' is required for bean 'circleId'

Autowired Annotation

  1. @Autowired works by taking into consideration type,name,qualifier
  2. If type not found name will be considered

spring.xml(by Type)

<beans>
	<bean id="PointA" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="20"/>
	</bean>	
	<bean class="com.mugil.shapes.Circle" id="circleId"></bean>	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

spring.xml(by Name)

<beans>
	<bean id="center" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="20"/>
	</bean>	
	<bean class="com.mugil.shapes.Circle" id="circleId"></bean>	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

In above xml center is the name of instance variable in circle class
spring.xml(by Qualifier)

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd">		
	<bean id="pointA" class="com.mugil.shapes.Point">
		<qualifier value="circle"/>			
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean>	
	<bean class="com.mugil.shapes.Circle" id="circleId"></bean>	
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>

circle.java

public class Circle implements Shape{
	@Autowired
	@Qualifier("circle")
	private Point center;
	
	@Override
	public void drawShape() {
		System.out.println("Shape of Circle ");
		System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY());
	}

	public Point getCenter() {
		return center;
	}
	
	public void setCenter(Point center) {
		this.center = center;
	}		
}

Reading constant from properties file
constant.properties

PointA.PointX=-20
PointA.PointY=0

spring.xml

<beans>
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="${PointA.PointX}"/>
		<property name="y" value="${PointA.PointY}"/>
	</bean>
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="constant.properties"></property>
	</bean>
</beans>

Using Interface
spring.xml

<beans>
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="${PointA.PointX}"/>
		<property name="y" value="${PointA.PointY}"/>
	</bean>
	<bean class="com.mugil.shapes.Circle" id="circleId">
		<property name="center" ref="pointA"/>
	</bean>
</beans>

shape.java

public interface Shape { 
	public void drawShape();
}

triangle.java

public class Triangle implements Shape
{
 public void drawShape()
 {
   System.out.println("Shape of Triangle");
 }
}

circle.java

public class Circle implements Shape{
	private Point center;
	
	@Override
	public void drawShape() {
		System.out.println("Shape of Circle ");
		System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY());
	}

	public Point getCenter() {
		return center;
	}

	public void setCenter(Point center) {
		this.center = center;
	}		
}

The objShape will call the draw method based on the instance referenced at runtime
shape.java

public class DrawingApp {
	public static void main(String[] args)  {
		 ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
		Shape objShape =  (Shape)objContext.getBean("circleId");
		objShape.drawShape();
	}
}

ApplicationContextAware and BeanNameAware

public class Triangle implements ApplicationContextAware
{
.
.
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException 
{
  this.objContext = arg0;		
  System.out.println("Called when Object to new ClassPathXmlApplicationContext('spring1.xml') is created");
}
}
public class Triangle implements BeanNameAware 
{
 .
 .
 .
 @Override
 public void setBeanName(String arg0) 
 {	
  this.strTriangle = arg0;
  System.out.println(this.strTriangle);
 }
}

When it is Invoked
Shape.java

//Application System out would be Printed
ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");

//this.strTriangle would be printed
BeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangleName");

Inheriting Bean Definition
spring.xml

<beans>
	<bean id="parentTriangle" class="com.mugil.shapes.Triangle">
		<property name="pointA">
			<ref bean="pointA"/>
		</property>
	</bean>
	<bean id="triangleId" class="com.mugil.shapes.Triangle" parent="parentTriangle">
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>
<beans>

Above the bean parentTriangle is inherited by the child bean triangleId

spring.xml
Bean definition can also be made as abstract by using abstract=”true” like one below

<beans>
	<bean id="parentTriangle" class="com.mugil.shapes.Triangle" abstract="true">
		<property name="pointA">
			<ref bean="pointA"/>
		</property>
	</bean>
</beans>

Managing Lifecycle of Bean

  1. Note the object for Context objContext is referred using AbstractApplicationContext not ApplicationContext
  2. registerShutdownHook() registers a hook which gets called at the end of application for cleanup

Shape.java

 AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
 objContext.registerShutdownHook();

Triangle.java

public class Triangle implements InitializingBean, DisposableBean 
{
 @Override
 public void destroy() throws Exception {
   System.out.println("DisposableBean Called");
 }

 @Override
 public void afterPropertiesSet() throws Exception {
   System.out.println("InitializingBean Called");
 }
}

We can all initialize the methods which should be called for initialization in spring.xml as below
spring.xml

<bean id="triangleId" class="com.mugil.shapes.Triangle" init-method="myInit" destroy-method="myDestroy">
                <property name="pointA" ref="pointA"/>
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>

Triangle.java

public class Triangle implements InitializingBean, DisposableBean 
{
  public void myInit()
  {
    System.out.println("Custom Init Method");
  }
	
  public void myDestroy()
  {
    System.out.println("Custom Destroy Method");
  }
}

Priority of method call when it is defined by using XML and Interface implementation

  1. Init method of XML will be called
  2. Custom Init method of Interface will be called
  3. Destroy method of XML will be called
  4. Custom Destroy method of Interface will be called

Bean Post Processor

  1. Will work before and after bean initialization
  2. Works only when called using application initialization of bean. Does not work with setter initialization
  3. called for every initialization of parent and child bean in class

Bean Post Processor

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
	    <property name="pointA" ref="pointA"/>
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>
	
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="-20"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="pointB" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="pointC" class="com.mugil.shapes.Point">
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean>
	
	<bean class="com.mugil.shapes.BeanInitialization"/>
	
</beans>

BeanInitialization.java

public class BeanInitialization implements BeanPostProcessor
{
	@Override
	public Object postProcessAfterInitialization(Object obj, String objName) throws BeansException 
       {
	  System.out.println("After Initialization of " + objName);
	  return obj;
	}

	@Override
	public Object postProcessBeforeInitialization(Object obj, String objName) throws BeansException 
       {
	  System.out.println("Before Initialization of " + objName);
	  return obj;
	}

}

So the above code runs four times for bean initialization of pointA,pointB,pointC and Triangle

shape.java

public class Shape 
{ 
  public static void main(String[] args)  
  {
    ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
    Triangle objTriangle2 =  (Triangle)objContext.getBean("triangleId");
    . 
    .
    .
}

BeanFactoryPostProcessor initialization happens before the beans gets initialized in the bean factory.

BeanInitialization2.java

public class BeanInitialization2 implements BeanFactoryPostProcessor
{
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException 
  {
     System.out.println("This is Bean factory Post Processor");
  }
}

spring.xml

<beans>
  <bean class="com.mugil.shapes.BeanInitialization2"/>	
</beans>

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="triangleId" class="com.mugil.shapes.Triangle"/>
</beans>

Triangle.java

package com.mugil.shapes;

public class Triangle {
	private String type;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void drawShape()
	{
		System.out.println("Shape of Triangle");
	}
}

Reading value of Bean by Application Context and Bean Factory

public class Shape 
{
   public static void main(String[] args) 
   {
	ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
	Triangle objTriangle1 =  (Triangle)objContext.getBean("triangleId");
	objTriangle1.drawShape();
		
		
	BeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
	Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangleId");
	objTriangle2.drawShape();
    }
}

Constructor Initialization
Triangle.java

package com.mugil.shapes;

public class Triangle 
{
	private String type;

        public Triangle(String ptype)
	{
		this.type = ptype;
	}
	
        public void drawShape()
	{
		System.out.println("Shape of Triangle");
	}
}

The Index Specifies which variable in the Bean is Initialized

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
		<constructor-arg index="0" value="Isolseles"/>
	</bean>
</beans>

Real World Dependency Injection
spring.xml

 
<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
		<property name="pointA" ref="point1"/>
		<property name="pointB" ref="point2"/>
		<property name="pointC" ref="point3"/>
	</bean>
	
	<bean id="point1" class="com.mugil.shapes.Point">
		<property name="x" value="-20"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="point2" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="point3" class="com.mugil.shapes.Point">
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean>
	
</beans>

Triangle.java

 
public class Triangle {
	private String type;
	private Point pointA;
	private Point pointB;
	private Point pointC;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void drawShape()
	{
		System.out.println("Shape of Triangle");
	}

	public Point getPointA() {
		return pointA;
	}

	public void setPointA(Point pointA) {
		this.pointA = pointA;
	}

	public Point getPointB() {
		return pointB;
	}

	public void setPointB(Point pointB) {
		this.pointB = pointB;
	}

	public Point getPointC() {
		return pointC;
	}

	public void setPointC(Point pointC) {
		this.pointC = pointC;
	}
}

Point.java

 
package com.mugil.shapes;

public class Point {
	private int x;
	private int y;
	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
}

Shape.java

 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Shape 
{
    public static void main(String[] args) 
    {		
	BeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
	Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangleId");
		
	System.out.println("The Refereeed Points are = ");
	System.out.println("Point A :" + objTriangle2.getPointA().getX() +" " + objTriangle2.getPointA().getY());
	System.out.println("Point B :" + objTriangle2.getPointB().getX() +" " + objTriangle2.getPointB().getY());
	System.out.println("Point C :" + objTriangle2.getPointC().getX() +" " + objTriangle2.getPointC().getY());		
	}
}

Incase the value of the bean wont be referred else where you can define the bean property simple as below

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
		<property name="pointA">
			<bean id="point1" class="com.mugil.shapes.Point">
				<property name="x" value="-20"/>
				<property name="y" value="0"/>
			</bean>			
		</property>
		<property name="pointB" ref="point2"/>
		<property name="pointC" ref="point3"/>
	</bean>

instead of

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
		<property name="pointA" ref="point1"/>
		<property name="pointB" ref="point2"/>
		<property name="pointC" ref="point3"/>
	</bean>
	
	<bean id="point1" class="com.mugil.shapes.Point">
		<property name="x" value="-20"/>
		<property name="y" value="0"/>
	</bean>
.
.
.

Using Alias

<bean id="triangleId" class="com.mugil.shapes.Triangle" name="triangleName">
.
.
.
</bean>
<alias name="triangleId" alias="triangle-alias"/>

In Java we can refer either by name or by alias as below
Using Alias

.
.
Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangleName");
(or)
Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangle-alias");
.
.

Using List
Triangle.java

public class Triangle 
{
    private List<Point> points;	
	
    public List<Point> getPoints() 
    {
	return points;
    }

    public void setPoints(List<Point> points) 
    {
 	this.points = points;
    }			
}

spring.xml

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle" name="triangleName">
		<property name="points">
			<list>
				<ref bean="point1"/>
				<ref bean="point2"/>
				<ref bean="point3"/>
			</list>		
		</property>		
	</bean>
	<bean id="point1" class="com.mugil.shapes.Point">
			<property name="x" value="-20"/>
			<property name="y" value="0"/>
	</bean>
	<bean id="point2" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="point3" class="com.mugil.shapes.Point">
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean>
</beans>

Shape.java

List<Point> arrPoints = objTriangle2.getPoints();
		
 for (Point objPoint : arrPoints) 
 {
   System.out.println("Point :" + objPoint.getX() +" " + objPoint.getY());
 }

Autowiring
Autowiring can be done based on name as below, byType and byConstructor.

<bean id="triangleId" class="com.mugil.shapes.Triangle" name="triangleName" autowire="byName">	
</bean>
<bean id="pointA" class="com.mugil.shapes.Point">
  <property name="x" value="-20"/>
  <property name="y" value="0"/>
</bean>
<bean id="pointB" class="com.mugil.shapes.Point">
  <property name="x" value="0"/>
  <property name="y" value="0"/>
</bean>
<bean id="pointC" class="com.mugil.shapes.Point">
  <property name="x" value="20"/>
  <property name="y" value="0"/>
</bean>

The Name of the instanceVariable in class should match the autowired xml bean Name.
Triangle.java

 
public class Triangle {	
	private Point pointA;
	private Point pointB;
	private Point pointC;
.        
.
}