Spring.net configuration in App.config

I recently (10/2012) converted my TFS build monitor program to be Dependency Injected using the Spring.Net container. I did this because I wanted a no-code way of setting certain parameters and of specifying a build device management class without having to convert a configuration parameter into a class name. I instead injected the spring object with the same name as the configuration parameter using Spring.Net's built in facilities.

Spring.Net is moving towards more annotation based configuration but the XML configuration is still widely used and powerful.  You can easily configure Spring.Net in XML with markup inserted into App.config.  The first thing to do is to tell the system that there are going to be some spring.Net sections added to app.config.  This defines two sections, contexts and objects.  context is the section that describes where to find other pieces.  objects is the section that contains the actual object wiring.  Additional sections might be present if you are using the AOP runtime injection facilities in Spring.Net


    
    
      

The spring/context section describes the object configuration location, in this case in the config file (App.config) in the spring/objects section. The first object defined in objects is the VariablePlaceholderConfigurer. This class implements variable replacement from properties for spring.Net object parameters.


  
    
      
    
    
      
        
          
            
              
            
          
        
      
The following configuration creates a TfsBuildConnection instance under the name myBuildServerConnection. the "${Tfs.Url}" notation is the spring expression language replacement where the value will be the application property TFS.Url.
      
        
        
        
        
      


Consumers of a spring wired object can either have it directly injected through auto-wiring or they can retrieve the object from the spring context in a Service Locator pattern. This following cod retrieves the build server connection created above. The IApplicationContext should only be created once and can support multiple hierarchical context files. This context creation syntax uses the spring configuration from App.config (or web.config) when find the location for spring defined objects.
     IApplicationContext ctx = ContextRegistry.GetContext();
    TfsBuildConnection myBuildConnect =
        ctx.GetObject("myBuildServerConnection") as TfsBuildConnection;

More complete example

The following listing is extracted from a sample App.config that contains the spring.net.   The full file is available on github https://github.com/freemansoft/build-monitors/tree/master/build-lights-net That file contains additional log4net and other configuration.

  
  
    
    
      
An example that demonstrates simple IoC features. " lazy-init="true" >
Thanks for reading...

Comments

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs