Spring中使用的ApplicationContextAware不会创建bean的新实例

时间:2014-06-17 02:12:46

标签: java spring applicationcontext

我所知道的是ApplicationContextAware每次用getBean时都会用来获取bean的新实例。

我创建了一个类Triangle,它是singleton,它的类成员变量为" prototype "

现在我想在每次创建Triangle的新对象时获取类成员的新实例。

但它不是发生了什么......

我将我的整个代码粘贴在下面::

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

1 个答案:

答案 0 :(得分:1)

我认为问题在于你的getPointA()方法根本没有被调用(你可以通过在那里放置一个日志语句来验证)。因为您通过XML注入对pointA的引用,所以draw方法明显使用该注入的实例。测试它的另一种方法是将draw()实现为:

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