尽管这个解决方案是如此明显,我本不应该发布它,但我将其保留作为提醒和对他人有用的参考点。
以下是我的app.config文件中的内容:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
接下来是:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
然后在我的应用程序中,我有:
using Spring.Context;
using Spring.Context.Support;
public partial class AlbumChecker : Window
{
private DataTable dataTable;
private Library library;
private Thread libraryThread;
public AlbumChecker()
{
InitializeComponent();
CreateToolTips();
IApplicationContext ctx = ContextRegistry.GetContext();
library = (Library)ctx.GetObject("mediaLibrary");
// Other initialisation
}
// Other code
}
所有的代码都能够很好地编译,但是,在调用GetContext()时,我遇到了一个异常:
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
我已经查阅了Spring.NET文档,但并没有发现我做错了什么——但显然我肯定做错了什么,否则就不会引发异常了!
AlbumLibraryWPF
是命名空间,AlbumLibraryWPF.AlbumLibrary
是我想实例化的类的完全限定名称。我猜测可能是这里出了问题,但是我无法确定具体错误在哪里。