问题
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?
回答1:
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 :
- In .NET configuration file (app.config/web.config) http://www.springframework.net/doc-latest/reference/html/resources.html#d4e2911
- In Spring configuration files http://www.springframework.net/doc-latest/reference/html/objects.html#context-custom-resourcehandler
Use it! resource="myfile://\server\share\${computername}\SpringConfig.xml"
回答2:
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)
来源:https://stackoverflow.com/questions/9295061/is-it-possible-to-have-a-dynamic-resource-path-for-import