2012-06-13 5 views
1

저는 Spring을 처음 접했습니다. 두 개의 속성을 가진 bean이 필요합니다. 두 번째 속성은 첫 번째 속성을 참조하는 인라인 bean입니다. 이런 식으로 뭔가 :Spring Bean을 구성하여 자체 등록 정보를 다시 사용 하시겠습니까?

<bean id="aBean" class="com.sample.Bean"> 
    <property name="propertyOne" value="something" /> 
    <property name="propertyTwo"> 
     <bean class="com.sample.AnotherBean"> 
      <property name="propertyThree" ref="propertyOne /> 
     </bean> 
    </property> 
</bean> 

자신의 빈 여기에 옵션이 아닙니다 propertyOne 만들기. 이것을 달성하는 가장 좋은 방법은 무엇입니까? 감사!

답변

1

공용 속성에 대한 빈을 만들고이 공용 속성 (BeanAnotherBean)을 참조하는 것이 유일한 방법입니다.이 옵션이없는 이유는 무엇입니까?

종속성 그래프 때문에 다른 방법으로는 작동하지 않습니다. aBean은 Another Bean에 종속되어 있으므로 AnotherBeanaBean 전에 인스턴스화되고 자식 bean 특성을 참조 할 수 없습니다. 이 종속성이 없었다면, 당신은 속성을 참조하는 스프링 EL 사용할 수도

:

<property name="propertyThree" value="${aBean.propertyOne}"/> 
+0

이입니다. 나는 Spring이 좀 더 우아한 솔루션을 제공하기를 바랬지 만, 만약 내가 작업하고 있던 어플리케이션이 Spring만큼 우아한다면, 처음에는이 문제를 갖지 않을 것이다. – Michael

1

당신은 별도의 콩으로 "propertyOne"를 만들 수 있습니다.

및 참조 사항 : aBean 및 사용자 인라인 빈. 나는이 문제를 해결 얻고 있었다 방법

<bean id="propertyOne" class="java.lang.String"> 
    <constructor-arg><value>"blabla"</value></constructor-arg> 
</bean> 

<bean id="aBean" class="com.test.SimpleBean"> 
<property name="name" ref="firstProperty" /> 
<property name="newBean"> 
    <bean class="com.test.OtherSimplwBean"> 
     <property name="otherName" ref="propertyOne" /> 
    </bean> 
</property> 

+1

응답 해 주셔서 감사합니다. 불행히도, 내 질문에 언급 한 것처럼, 내 응용 프로그램에서 옵션이 될 수 없습니다. – Michael

관련 문제