09 March 2007

Spring, Nhibernate, log4net - PART I

Finally I had some more time to bound these tools. First thing which I did was launching the spring. It was quiet easy to do. I downloaded the newest version of the framework from here.
After unzipping the package I created a new console project where I added the new references from spring framework (Spring.Core, Common.Logging, Common.Logging.Log4Net). To make this framework working the first thing which I had to do was adding App.config (application configuration file). This file is a main configuration file which can be split into sections. Spring uses couple sections like context, objects, parsers, resources and many others. For my example I will use only context and objects. In first section you are able to define where the objects definition is, the second one define all objects uses in application. To get more info refer to www.springframework.org. To don't waste more time I will paste my configuration file:

<configuration>

<configsections>

<sectiongroup name="spring">

<section name="context" type="Spring.Context.Support.ContextHandler , Spring.Core">

<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core">

</section>

</section>

<spring>

<context>

<resource uri="config://spring/objects">

</resource>

<objects xmlns=" http://www.springframework.net">

</objects>

</context>

</spring>
</sectiongroup>

</configsections>

</configuration>


As you can see in my simple configuration file there is no objects defined yet. They will be add later on. Another thing which I would you like to recommend is adding the definition schema of spring objects to the system. This will avoid making the mistakes that you probably made while defining objects. To do it you have to simply copy the file spring-objects.xsd to {Microsoft Visual Studio 8}\Xml\Schemas, after that restart the visual studio environment.
Ok when my configuration file is prepared it can build the IApplicationContext. This interface provides the same functionality as the IObjectFactory and complements it which features as such AOP and message resources. To get more info go to spring homepage. To get the IApplicationObject you have to in the begging of the Main method type something like that:

static void Main()
{
IApplicationContext ctx= ContextRegistry.GetContext();
}

Good habit is wrapping ctx object as public properties, to be available for all classes.
In next part of this article I will bring closer the log4net and nhibernate sections.

Labels: ,