Spring.net inject Dictionary.
http://stackoverflow.com/questions/4591638/spring-net-ref-to-dictionary-item
<object name="Paths" id="Paths" type="System.Collections.Generic.Dictionary<string,string>">
<constructor-arg>
<dictionary key-type="string" value-type="string">
<entry key="cfgFile">
<value>config.txt</value>
</entry>
</dictionary>
</constructor-arg>
</object>
You can use the @(object-id-here) expression syntax to retrieve an object from the Spring context using an expression.
@(object-id-here)
<object name="obj1" type="MyTestClass" depends-on="Paths">
<property name="cfg" expression="@(Paths)['cfgFile']"/>
</object>
Edit - below is the answer that was accepted
I can imagine that you would like this kind of configuration to be available in your app.config
. If so, you can use the PropertyPlaceholderConfigurer
; see the example in section 5.9.2.1.
In your case, it would look something like this:
<!-- 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>
And your myconfig.xml
contains:
<!-- ... -->
<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>
<!-- ... -->
Note that instead of app.config
, you can use any other IResource
.
An alternative solution is to define the cfgFile
as an object in your configuration and then in your dictionary reference this object using value-ref
(see spring docs 5.3.2.4 on how to do this). But this (probably) isn't what you are looking for, since you're injecting primitive values (so it's not worth the effort of creating an explicit ConfigurationObject
).
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey, TValue> class. You can access elements either by the key or by the index
spring注入的一些其它示例:
from:
http://www.cnblogs.com/Clingingboy/archive/2009/08/25/1553561.html
集合属性(list
, set
, name-values
and dictionary
)
<property name="SomeList"> <list> <value>a list element followed by a reference</value> <ref object="myConnection"/> </list> </property> <!-- results in a call to the setter of the SomeDictionary (System.Collections.IDictionary) property --> <property name="SomeDictionary"> <dictionary> <entry key="a string => string entry" value="just some string"/> <entry key-ref="myKeyObject" value-ref="myConnection"/> </dictionary> </property> <!-- results in a call to the setter of the SomeNameValue (System.Collections.NameValueCollection) property --> <property name="SomeNameValue"> <name-values> <add key="HarryPotter" value="The magic property"/> <add key="JerrySeinfeld" value="The funny (to Americans) property"/> </name-values> </property> <!-- results in a call to the setter of the SomeSet (Spring.Collections.ISet) property --> <property name="someSet"> <set> <value>just some string</value> <ref object="myConnection"/> </set> </property>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步