开发者

spring.net used in a class library

I am attempting to use spring.net 's IoC conatiner in a class library which in and of itself is not an executable. A web project simply calls this library , this library contains the references to the spring binaries and to spring's config files. Essentially开发者_如何转开发 the question is: Does spring.net need to reside in an executable to start, or canit reside in a classs library that will be referenced by an executable?

Any help will be appreciated.


You can include part of your configuration in the class library project as an embedded resource file. Let's say you called it LibraryConfig.xml. Then in your executable's application config file, you include the embedded resource using the assembly: prefix. Here's an example:

<spring>
  <context type="Spring.Context.Support.XmlApplicationContext, Spring.Core">
    <resource uri="assembly://FooLibrary/FooLibrary/LibraryConfig.xml"/>
    <resource uri="config://spring/objects" />
  </context>
  <objects xmlns="http://www.springframework.net">
    <object id="mainForm" type="FooApp.MainForm, FooApp">
      <!-- mainController is some object defined in LibraryConfig.xml -->
      <property name="Controller" ref="mainController"/>
    </object>
  </objects>
</spring>

If your main application doesn't need to use Spring itself, I think you can set up the whole application context in the library. Embed the config file as described above, then define a singleton object to hold the application context and load it from the embedded config file. Finally, you need to define some kind of factory methods for the client code to create your classes with. The factory methods can either go on the singleton itself (probably using generics), or have a separate factory method on each class that needs to be instantiated. Those factory methods make the actual requests from the application context and the client code never sees it.


it can reside in a dll which is referenced by an executable, but make sure that the configuration is included in (or referenced by) the executable's config file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜