Dashboard > Spring.NET > Home > Type Aliases
  Spring.NET Log In View a printable version of the current page.  
  Type Aliases
Added by Aleksandar Seovic, last edited by Mark Pollack on Nov 27, 2005  (view change)
Labels: 
(None)

Warning

This documentation has been moved to docbook format and now resides on the Spring.NET website. If you feel the need to edit this document please send and email to the Spring.NET developer list.

Type aliases allow you to simplify Spring configuration file by replacing fully qualified type name with an alias for frequently used types. Aliases can be registered both within config file and programatically and can be used anywhere in the context config file where fully qualified type name is expected.

Configuring Type Aliases

In order to configure type aliases you need to define custom config section in the Web/App.config file for your application, as well as the custom configuration section handler:

<configuration>

   <configSections>
     <sectionGroup name="spring">
       <section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/>
       ...
     </sectionGroup>
   </configSections>

   <spring>

     <typeAliases>
       <alias name="WebServiceExporter" type="Spring.Web.Services.WebServiceExporter, Spring.Web"/>
       <alias name="DefaultPointcutAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop"/>
       <alias name="AttributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop"/>
       <alias name="CacheAttribute" type="Spring.Attributes.CacheAttribute, Spring.Core"/>
       <alias name="MyType" type="MyCompany.MyProject.MyNamespace.MyType, MyAssembly"/>
       ...
     </typeAliases>

     ...

   </spring>

</configuration>

Once you have aliases defined, you can simply use them anywhere where you would normally specify fully qualified type name:

<object id="MyWebService" type="WebServiceExporter">
    ...
</object>

<object id="cacheAspect" type="DefaultPointcutAdvisor">
    <property name="Pointcut">
        <object type="AttributePointcut">
            <property name="Attribute" value="CacheAttribute"/>       
        </object>
    </property>
    <property name="Advice" ref="aspNetCacheAdvice"/>
</object>

...

Registering Type Aliases Programmatically

Programmatic alias registration is mainly intended to be used internally within the framework to pre-register aliases for the commonly used types. However, it can be used within custom application context implementations and within third party libraries that are built on top of Spring.

Registering aliases programmatically is very simple – all you need to do is call one of the static RegisterType methods on the TypeRegistry singleton:

TypeRegistry.RegisterType("WebServiceExporter", "Spring.Web.Services.WebServiceExporter, Spring.Web");
TypeRegistry.RegisterType("CacheAttribute", typeof(CacheAttribute));
...

As you can see, you can either pass a fully qualified type name as a second parameter and let Spring resolve it for you, or you can specify the type directly.

Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators