Spring 从上下文感知检索的应用程序上下文未正确生成Bean
以下是我的Pojo:Spring 从上下文感知检索的应用程序上下文未正确生成Bean,spring,dependency-injection,applicationcontext,Spring,Dependency Injection,Applicationcontext,以下是我的Pojo: public class Triangle implements ApplicationContextAware, BeanNameAware{ private Point pointA; private Point pointB; private Point pointC; My springs.xml的结构如下所示: <bean id="pointA" class="com.betta.springtest.Point">
public class Triangle implements ApplicationContextAware, BeanNameAware{
private Point pointA;
private Point pointB;
private Point pointC;
My springs.xml的结构如下所示:
<bean id="pointA" class="com.betta.springtest.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
<bean id="pointB" class="com.betta.springtest.Point">
<property name="x" value="-20" />
<property name="y" value="0" />
</bean>
<bean id="pointC" class="com.betta.springtest.Point">
<property name="x" value="20" />
<property name="y" value="0" />
</bean>
<bean id="triangle-bean" class="com.betta.springtest.Triangle" autowire="autodetect"/>
<alias name="triangle-bean" alias="triangle" />
在上面的代码中,我希望对象点B具有springs.xml中设置的值,这是我无法获得的。我是Spring新手,有人能帮我理解这个概念吗?Springbeans默认为单例。这意味着每个应用程序上下文实例只有一个bean实例。如果您修改了从应用程序上下文检索的实例,那么当您再次检索它时,它仍然会被修改,因为它只存在一个实例 您可以使用prototype bean范围,这意味着每次从应用程序上下文检索新的bean实例时,都会创建新的bean实例,在这种情况下,代码的行为将与您预期的一样
<bean id="pointA" class="com.betta.springtest.Point" scope="prototype">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
<bean id="pointA" class="com.betta.springtest.Point" scope="prototype">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>