Configure
- Add to References
- Spring.Core.dll
- Add to App.config or Web.config
<sectionGroup name="spring"> <section name="context" type="Spring.Context.Support .ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support .DefaultSectionHandler, Spring.Core" /> </sectionGroup>
- Define an object interface
public interface ISimpleMessage { String Message { get; } }
- Do an implementation
public class SimpleMessage : ISimpleMessage{ private String _message; public SimpleMessage(){} public SimpleMessage(String message) { _message = message; } public String Message { get { return _message; } set { _message = value; } } }
- Map objects in App.config
<spring> <context> <resource uri="config://spring/objects" /> </context> <objects xmlns= "http://www.springframework.net"> <!-- Messages --> <object name="SimpleMessage" type="Test.Spring.SimpleMessage, Test.Spring" singleton="false"> <constructor-arg name="message" value="constructor-based injection." /> </object> <object name="SimpleMessageByProperty" type="Test.Spring.SimpleMessage, Test.Spring" singleton="false"> <property name="Message" value="property-based injection." /> </object> </objects> </spring>
- Use
using Spring.Context; using Spring.Context.Support; ... IApplicationContext ctx = ContextRegistry.GetContext(); ISimpleMessage smsg = (ISimpleMessage) ctx.GetObject("SimpleMessage"); label1.Text = smsg.Message; ...
- Constructor injection
<object name="SimpleMessage" type="Test.Spring.SimpleMessage, Test.Spring" singleton="false"> <constructor-arg name="message" value="constructor-based injection." /> </object>
- Property injection
<object name="SimpleMessageByProperty" type="Test.Spring.SimpleMessage, Test.Spring" singleton="false"> <property name="Message" value="property-based injection." /> </object>
- IMessageSource
- For retrieving localized resources
- Text
- Image
- Add a resouce file called TestResource.resx
- Add a resouce file called TestImages.resx
- Add to App.config
- Use
// Default culture label1.Text = ctx.GetMessage("FirstName", CultureInfo.CurrentCulture); //Display spanish CultureInfo spanish = new CultureInfo("es"); label1.Text = ctx.GetMessage("FirstName", spanish); // Get image pbxTest.Image = ctx.GetResourceObject( "Sunset") as Image;
<object name="messageSource" type="Spring.Context.Support .ResourceSetMessageSource, Spring.Core"> <property name="resourceManagers"> <list> <value>Test.Spring.TestImages, Test.Spring</value> <ref object="myResourceManager" /> </list> </property> </object> <object name="myResourceManager" type="Spring.Objects.Factory .Config.ResourceManagerFactoryObject, Spring.Core"> <property name="baseName" value="Test.Spring.TestResource" /> <property name="assemblyName" value="Test.Spring" /> </object>
- For retrieving localized resources
References