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
: 그것이 실행되는 ApplicationContext를 통보하고자하는 모든 객체에 의해 구현되는 "인터페이스를 테스트하는 다른 방법으로) 무승부 (의 구현을하는 것입니다 에서." "BeBean을 얻을 때마다 콩의 새로운 인스턴스를 얻는 데 익숙하다."라는 생각을 어디에서 얻었습니까? –
공정하게 - @AleZalazar ApplicationContextAware를 통해 얻은 컨텍스트를 사용하고 .getBean을 호출하면 매번 콩의 새 인스턴스가 생성됩니다. 맞습니까? – Kilokahn
Triangle4 클래스는 어떻게 만듭니 까? Triangle4는 bean이어야합니다 (Triangle4의 주석 @Component 설정). 또한 컨텍스트에서 bean을 가져와야합니다. – Malahov