Tuesday, 22 February 2011

Spring.Net loading xml assembly resources

This might contradict my previous post, but then again, it is a helpful tip in case you have a lot of spring.net xml configuration files.

At some stage in your project, you might want to include all your xml configuration files for spring as embedded resources inside your assembly. This is useful during deployment as you won't need to make sure that your xml files are deployed alongside your code base.

But, your spring configuration sections might start to have quite a few lines to indicate which resources to load.

What I wanted was for spring to load all xml embedded configuration files under a namespace instead so that it would look something like this:

And so I dived into Spring.Net code base to figure out where the resources where being loaded up and if I could intercept that call to add the extra resources I wanted. This wasn't easy to find, but in the end I succeeded in what I wanted to do.

What you need is to override the method InstantiateContext of the context handler in a subclass of your own (don't forget to change the configuration section once you have done so).

The resources array you get passed in is a list of resources definition you have configured under the spring/context section (see above).

The next bit, was easy, all I needed to do was to parse the resource strings with '*' and load up all the resources under the path I specified. The new array I returned contained the full list of resources I wanted spring to load up.

That's it.

Now, if you want to take an easier root, you can download the FluentSpring API dll which contains the classes you need right here.

No comments: