是否可以有一个动态资源路径用于导入?
我有一个带有配置文件的 Spring.NET 程序。为了顺利地从测试到产品的过渡,我想要一个环境变量来提供共享配置文件的路径,并将其用于 导入资源,但它显示为
不是已解决。例如,如果我尝试加载文件
我会得到一个文件未找到异常,如下所示:
System.Configuration.ConfigurationErrorsException:创建上下文“spring.root”时出错:文件 [\server\share\${computername}\SpringConfig.xml] 无法解析为本地文件路径 - 资源不使用“file:”协议。 ---> Spring.Objects.Factory.ObjectDefinitionStoreException: IOException 从文件 [\server\share\${computername}\SpringConfig.xml] 解析 XML 文档 ---> System.IO.FileNotFoundException:文件 [\server\share\${computername}\SpringConfig.xml] 无法解析为本地文件路径 - 资源不使用“file:”协议。
有没有办法可以在 Spring.NET 中拥有动态导入路径,最好无需编写代码?
I have a Spring.NET program with a configuration file. To smooth the transition from test to prod I'd like to have an environment variable that gives the path to a shared config file, and use that to import a resource, however it appears the <import resource="path"/>
is not being resolved. For example if I try to load the file <import resource="\\server\share\${computername}\SpringConfig.xml"/>
I get a file not found exception as below:
System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': file [\server\share\${computername}\SpringConfig.xml] cannot be resolved to local file path - resource does not use 'file:' protocol. ---> Spring.Objects.Factory.ObjectDefinitionStoreException: IOException parsing XML document from file [\server\share\${computername}\SpringConfig.xml] ---> System.IO.FileNotFoundException: file [\server\share\${computername}\SpringConfig.xml] cannot be resolved to local file path - resource does not use 'file:' protocol.
Is there a way I can have a dynamic import path in Spring.NET, preferably without writing code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何,您可以使用一些额外的代码来完成此操作:
创建您自己的 FileSystemResource,它将替换资源名称中的占位符。从覆盖现有 FileSystemResource(在 Spring.Core.IO 命名空间中)开始
使用您自己的协议名称(例如:myfile://)在容器中注册新的 IResource 实现,请参阅此处的参考文档以获取示例:
http://www.springframework.net/doc-latest/reference /html/resources.html#d4e2911
http://www.springframework.net/doc -latest/reference/html/objects.html#context-custom-resourcehandler
使用它!
resource="myfile://\server\share\${computername}\SpringConfig.xml"
You can do that anyway with some extra code:
Create your own FileSystemResource that will replace placeholders in the resource name. Start from overriding the existing FileSystemResource (In Spring.Core.IO namespace)
Register your new IResource implementation in the container using your own protocol name (ex: myfile://) See ref docs here for an example :
http://www.springframework.net/doc-latest/reference/html/resources.html#d4e2911
http://www.springframework.net/doc-latest/reference/html/objects.html#context-custom-resourcehandler
Use it!
resource="myfile://\server\share\${computername}\SpringConfig.xml"
我认为当前版本无法做到这一点。
最新的Java版本支持它,所以我们可以期待未来版本中的这个功能(默认使用变量环境)
I don't think we can do that with the current version.
Latest Java version supports it, so we can expect this feature in a future version (Using variables environnement by default)