user3746835
user3746835

Reputation: 11

ApplicationContextAware used in spring does not create a new instance of the beans

All I know is that ApplicationContextAware is used to get a new instance of the bean everytime I do getBean.

I created a class Triangle which is singleton, and its class members variables as "prototype"

Now I wanted to get new instances of the class members everytime I create a new object of Triangle.

But its not what is happening...

I am pasting my entire code below::

package com.sonal.javabrains;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Triangle4 implements ApplicationContextAware
{


    private Point pointA;
    private Point pointB;
    private Point pointC;
    private ApplicationContext context ;


    public Point getPointA() {
        return (Point) context.getBean("pointA");
    }
    public void setPointA(Point pointA) {
        this.pointA = pointA;
    }
    public Point getPointB() {
        return (Point) context.getBean("pointB");
    }
    public void setPointB(Point pointB) {
        this.pointB = pointB;
    }
    public Point getPointC() {
        return (Point) context.getBean("pointC");
    }
    public void setPointC(Point pointC) {
        this.pointC = pointC;
    }

    public void draw()
    {
        System.out.println("Point A is :" + pointA.getX() + "," + pointA.getY()+"having references as"+pointA.hashCode());
        System.out.println("Point B is :" + pointB.getX() + "," + pointB.getY()+"having references as"+pointB.hashCode());
        System.out.println("Point C is :" + pointC.getX() + "," + pointC.getY()+"having references as"+pointC.hashCode());
    }
    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException {
        this.context = context;

    }

}


-----------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id = "triangle4" class = "com.sonal.javabrains.Triangle4">
<property name="pointA" ref = "pointA"></property>
<property name="pointB" ref = "pointB"></property>
<property name="pointC" ref = "pointC"></property>
</bean>
<bean id = "pointA" class = "com.sonal.javabrains.Point" scope = "prototype">
<property name = "x" value = "0" />
<property name = "y" value = "0" />
</bean>


<bean id ="pointB"  class  = "com.sonal.javabrains.Point" scope = "prototype">
<property name = "x" value = "-20" />
<property name = "y" value = "0" />
</bean>


<bean id ="pointC"  class  = "com.sonal.javabrains.Point" scope = "prototype">
<property name = "x" value = "0" />
<property name = "y" value = "20" />
</bean>


</beans>

-------------------------------------------------------------------------------
------------------------------------------------------------------------------


package com.sonal.javabrains;

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;
    }


}
--------------------------------------------------------------------------------


first Triangle is 748080913
Point A is :0,0having references as1626635253
Point B is :-20,0having references as1391870861
Point C is :0,20having references as634194056
second Triangle is 748080913
Point A is :0,0having references as1626635253
Point B is :-20,0having references as1391870861
Point C is :0,20having references as634194056

--------------------------------------------------------------------------------------------


answer when i declare triangle also as protype




first Triangle is 334936591
Point A is :0,0having references as724646150
Point B is :-20,0having references as748080913
Point C is :0,20having references as1626635253
second Triangle is 1391870861
Point A is :0,0having references as634194056
Point B is :-20,0having references as938159131
Point C is :0,20having references as815578443

Upvotes: 0

Views: 226

Answers (1)

Kilokahn
Kilokahn

Reputation: 2311

I think the problem is that your getPointA() method is not called at all (you can verify by placing a log statement in there). Because you are injecting the references to a pointA via XML, the draw method is plainly using that injected instance. A different way of testing it would be to have the implementation of draw() as:

public void draw()
{
    getPointA();
    getPointB();
    getPointC();
    System.out.println("Point A is :" + pointA.getX() + "," + pointA.getY()+"having references as"+pointA.hashCode());
    System.out.println("Point B is :" + pointB.getX() + "," + pointB.getY()+"having references as"+pointB.hashCode());
    System.out.println("Point C is :" + pointC.getX() + "," + pointC.getY()+"having references as"+pointC.hashCode());
}

Upvotes: 1

Related Questions