Quick Start
A very basic Spring.NET RichClient application just looks like any other Windows Forms application with the only difference, that before running your main form you have to initialize the application framework.
using System;
using Spring.Windows;
namespace SpringUtilsDemo {
internal static class Program {
[STAThread]
private static void Main() {
Application.Initialize();
Application.Run(new MainForm());
}
}
}
This alone doesn't provide you much benefit and you unlikely would want to use the Spring.NET RichClient framework just for the sake of it. So the next best thing you can do is specifying application contexts that should be used with your application.
Unlike other Spring.NET applications you might have encountered a Spring.NET RichClient application needs two application contexts - one during startup and one during runtime. This has the advantage that you can control the behavior of the whole application at a very early stage (during startup) while postponing more complex DI tasks to a later point. As a result the application startup time is drastically reduced and the user already can get some UI feedback during startup of the main application context. This is very important when developing desktop applications.
using System;
using Spring.Windows;
namespace SpringUtilsDemo {
internal static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main() {
string startup = "assembly://SpringUtilsDemo/SpringUtilsDemo.config/startup.xml";
string context = "assembly://SpringUtilsDemo/SpringUtilsDemo.config/applicationcontext.xml";
Application.Initialize(startup, context);
Application.Run(new MainForm());
}
}
}
In the startup context you can specify a lifecycle advisor for your application. This advisor allows you to intercept almost any action in the lifecylce of your application. The default implementation handles dependency injection into your GUI components, you can of course specify your own that e.g. injects a skin framework into your application to give your windows a more individual touch.
Optionally you can specify a splash screen that will be displayed during startup of the main application context.
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<description>SpringUtilsDemo Startup Context</description>
<object id="splashScreen" type="Spring.Windows.Splash.ProgressSplashScreen, Spring.Windows" />
<object id="applicationLifecycleAdvisor" type="Spring.Windows.Config.DefaultApplicationLifecycleAdvisor" />
</objects>
The main application context can be used just like any context in any other Spring.NET application. E.g. you can define a message source for your application.
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"
default-autowire="byName">
<description>SpringUtilsDemo Application Context</description>
<!-- Message Source -->
<object name="messageSource" type="Spring.Context.Support.ResourceSetMessageSource, Spring.Core">
<property name="ResourceManagers">
<list>
<value>SpringUtilsDemo.Messages, SpringUtilsDemo</value>
</list>
</property>
</object>
</objects>
Unlike other objects defined in the application context GUI components on a form will be instantiated in the forms InitializeComponents() method generated by the Windows Forms designer. Hence, if you want to inject some dependencies, you cannot write an ordinary <object> definition in your application context as the instantiated component would never be shown anywhere.
To achieve dependency injection into GUI components you have to specify object templates (i.e. <object> tags without a type attribute) thats name matches the ObjectId property of the GUI component. Per default this is the fully qualified class name of the component. By adding
<object name="SpringUtilsDemo.ExampleForm">
<property name="BottomLabel.Text" value="Spring.NET RichClient" />
</object>
to your application context, the specified string will be injected into the BottomLabel's Text property.
If you prefer automatic dependency injection by autowiring and don't want to hassle with specific components you can simply set the autowire property of your application context to your desired strategy and add
<object name="defaultDependencyTarget" />
to your application context.
