[英]ApplicationContextAware used in spring does not create a new instance of the beans
我所知道的是,每次執行getBean
,都會使用ApplicationContextAware
獲取該bean的新實例。
我創建了一個單例的Triangle類,其類成員變量為“ 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
我認為問題在於根本沒有調用您的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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.