2014-06-17 49 views
0

我所知道的是,ApplicationContextAware用於在每次我做getBean時獲得bean的新實例。在Spring中使用的ApplicationContextAware不會創建bean的新實例

我創建了一個類三角形是單身,和它的類成員變量爲「原型

現在我想獲得的類成員的新實例每次我創建三角的新對象。

但它不是發生了什麼......

我粘貼下面::

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

根據文檔了ApplicationContextAware是:「接口由希望被通知的ApplicationContext的,它運行的任何對象實現在。」你從哪裏得到了「每次使用getBean都習慣得到一個新的bean實例」的想法? –

+0

公平 - 如果使用通過ApplicationContextAware獲取的上下文並調用.getBean,@AleZalazar會每次獲得bean的新實例,對吧? – Kilokahn

+0

如何創建類Triangle4? Triangle4必須是bean(在Triangle4上設置註釋@Component),還必須從上下文獲取bean。 – Malahov

回答

1

我整個的代碼,我認爲問題是,你的getPointA()方法不是在所有(所謂的可以通過在那裏放置一個日誌語句來驗證)。因爲您通過XML將引用注入到pointA,所以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()); 
} 
相關問題