Spring.Net 引用字典项
我无法找到如何在 spring xml 文件中的其他位置使用字典(xml spring 配置)中定义的项目。我尝试了类似的方法,但在“obj1”的 init 中出现错误。
<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>
<object name="obj1" type="MyTestClass" depends-on="Paths">
<property name="cfg" expression="${Paths['cfgFile']}"/>
</object>
感谢您的帮助...
I can't find out how use an item defined in dictionary (xml spring config) at other place in the spring xml file. I tried something like this, but I ever get an error in init of "obj1".
<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>
<object name="obj1" type="MyTestClass" depends-on="Paths">
<property name="cfg" expression="${Paths['cfgFile']}"/>
</object>
Thanks for your help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我原来的答案被接受了,但现在我相信更好的答案是:
你的表达不正确,应该是:
你可以使用
@(object-id-here)
表达式语法使用表达式从 Spring 上下文中检索对象。编辑 - 下面是已接受的答案
我可以想象您希望在
app.config
中提供这种配置。如果是这样,您可以使用PropertyPlaceholderConfigurer
;请参阅第 5.9.2.1 节中的示例。在您的情况下,它看起来像这样:
并且您的
myconfig.xml
包含:请注意,您可以使用任何其他
IResource< /代码>。
另一种解决方案是将 cfgFile 定义为配置中的对象,然后在字典中使用 value-ref 引用该对象(请参阅 spring 文档 5.3.2.4 了解如何执行此操作)。但这(可能)不是您正在寻找的,因为您正在注入原始值(因此不值得花费精力创建显式的
ConfigurationObject
)。My original answer was accepted, but now I believe a better answer would be:
Your expression is not correct, it should be:
You can use the
@(object-id-here)
expression syntax to retrieve an object from the Spring context using an expression.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 thePropertyPlaceholderConfigurer
; see the example in section 5.9.2.1.In your case, it would look something like this:
And your
myconfig.xml
contains:Note that instead of
app.config
, you can use any otherIResource
.An alternative solution is to define the
cfgFile
as an object in your configuration and then in your dictionary reference this object usingvalue-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 explicitConfigurationObject
).