2011-01-04 5 views
3

사전 (xml spring config)에 정의 된 항목을 spring xml 파일의 다른 위치에서 사용하는 방법을 알 수 없습니다. 이런 식으로 시도했지만 "obj1"초기화시 오류가 발생합니다. 당신의 도움에 대한사전 항목에 대한 Spring.Net 참조

<object name="Paths" id="Paths" type="System.Collections.Generic.Dictionary&lt;string,string&gt;"> 
    <constructor-arg> 
     <dictionary key-type="string" value-type="string"> 
     <entry key="cfgFile"> 
      <value>config.txt</value> 
     </entry> 
     </dictionary> 
    </constructor-arg> 
    </object> 
    <object name="obj1" type="MyTestClass" depends-on="Paths"> 
    <property name="cfg" expression="${Paths['cfgFile']}"/> 
    </object> 

감사합니다 ...

+0

오류 메시지를 게시 할 수 있습니까? – Marijn

답변

2

내 원래의 대답은 받아 들여졌다, 그러나 지금은 더 좋은 대답이 될 것이라고 믿습니다

귀하의 표현이 정확하지 않습니다, 그것은해야한다 :

<object name="obj1" type="MyTestClass" depends-on="Paths"> 
    <property name="cfg" expression="@(Paths)['cfgFile']"/> 
</object> 

@(object-id-here)expression syntax to retrieve an object from the Spring context using an expression을 사용할 수 있습니다.

편집 - 아래 당신이 당신의 app.config에서 사용할 수있게 구성의이 종류를 좋아하는 것을 상상할 수

를 받아 들여졌다 답변입니다. 그렇다면 PropertyPlaceholderConfigurer을 사용할 수 있습니다. section 5.9.2.1의 예를 참조하십시오. 귀하의 경우에는

, 그것은 다음과 같이 보일 것입니다 :

<!-- app.config --> 
<configuration> 

    <configSections> 
    <sectionGroup name="spring"> 
     <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 
    </sectionGroup> 
    <section name="PathConfiguration" type="System.Configuration.NameValueSectionHandler"/> 
    </configSections> 

    <PathConfiguration> 
    <add key="cfgFile" value="config.txt"/> 
    <add key="otherCfgFile" value="otherconfig.txt"/> 
    </PathConfiguration> 

    <spring> 
    <context> 
     <resource uri="mycongfig.xml"/> 
    </context> 
    </spring> 

</configuration> 

그리고 당신의 myconfig.xml 다음이 포함 대신 app.config, 당신은 다른 IResource을 사용할 수 있습니다

<!-- ... --> 
<object name="obj1" type="MyTestClass"> 
    <property name="cfg" value="${cfgFile}"/> 
</object> 

<object name="appConfigPropertyHolder" 
     type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core"> 

    <property name="configSections">   
     <value>PathConfiguration</value>        
    </property>    

</object> 
<!-- ... --> 

참고.

대체 솔루션은 구성에 객체로 cfgFile을 정의한 다음 사전에서 value-ref을 사용하여이 객체를 참조하는 것입니다 (자세한 방법은 스프링 문서 5.3.2.4 참조). 그러나 이것은 원시 값을 주입하기 때문에 (아마도) ConfigurationObject을 만들려고 노력하지 않아도되므로 찾고있는 것과 다를 수 있습니다.

+0

그것이 내가 찾던 것입니다. 두 가지 접근법 모두 내 목적에 가치가 있습니다. –

+0

고마워, 마리 언. –

관련 문제