Adding object definitions from non-local classes

On the Spring forum someone asked if it was possible to add object definitions to the application context of classes which were beyond the searchable range of the application. I was a little bit sceptic at first but one debugging session later, I found out that it is possible to do this. Though I really don't support the whole idea of doing this.

When you are not using fully qualified assembly names in your definition Spring will look at the current loaded assemblies to resolve the type, so with this trick you can make it work. The topic can be found here and the source code can be found here, don't forget to change the path of the “ClassLibrary1” assembly to make it work.

class Program
{
	static void Main(string[] args)
	{
		Assembly.LoadFile(@"C:MyPathToSomeDllClassLibrary1.dll");
		object o = ContextRegistry.GetContext().GetObject("MyObject");
		Console.WriteLine("Retrieved object from context");
		Console.ReadLine();
	}
}

<objects xmlns="http://www.springframework.net" >
  <object id="MyObject"
          type="ClassLibrary1.Class1">
    <property name="Test" value="MyTestValue"/>
  </object>
</objects>

Leave a Reply

Your email address will not be published. Required fields are marked *