tags:

views:

30

answers:

3

I have a string property which looks similar to the following example:

<property name="mappingData">
  <list>                
    <bean class="com.company.product.longNamingStandard.migration.extractor.FieldMapping">
      <property name="elementName" value="entitlement.user"/>
      <property name="mapping" value="DocUsers"/>
    </bean>
    <bean class="com.company.product.longNamingStandard.migration.extractor.FieldMapping">
      <property name="elementName" value="entitlement.contributor"/>
      <property name="mapping" value="DocContributors"/>
    </bean>
  </list>
</property>  

The long class name(s) effect readability & also create a refactoring overhead.

Is it possible to alias the class name and use a short name to declare the beans? Or is there an alternate best practice I'm missing?

+1  A: 

each <bean/> comes with an attribute of name and id to help you reference those beans later in your configuration.

I would suggest using the id for declaring the bean.

your config could look like:

<bean id="fooBean" class="com.example.foo"/>
<bean id="barBean" class="com.example.bar"/>

<list>
   <ref>fooBean</ref>
   <ref>barBean</ref>
</list>
Bivas
A: 

Why not declare those inner beans as separate top-level beans with their own names, and then reference them in the list ?

Brian Agnew
Thanks Brian, I do this in some places, I should have included it in the example.I was hoping for a way to clean up the bean definitions in their own right. I'd prefer not to be using the class name 40 odd times in the XML file.
Troyzor
+1  A: 

You may try to represent your mapping in some short form, and then convert it to the list of FieldMappings. For example, mappings from your snippet may be represented as a map.

As a theoretic exercise in Spring 3 you can do this with Spring Expression Language (if FieldMapping has the apropriate constructor):

<util:map id = "m">
    <entry name = "entitlement.user" value = "DocUsers" />
    <entry name = "entitlement.contributor" value = "DocContributors" />
</util:map>
...
<property name = "mappingData" 
    value = "#{m.![new com.company.product.longNamingStandard.migration.extractor.FieldMapping(key, value)]}" />

If this expression is too obscure, you may implement a FactoryBean to take a short form of your mapping data (for example, a map, as in this example) and return a configured list of FieldMappings:

<property name = "mappingData">
    <bean class = "FieldMappingListFactoryBean">
        <property name = "mappings">
            <map>
                <entry name = "entitlement.user" value = "DocUsers" />
                <entry name = "entitlement.contributor" value = "DocContributors" />
            </map>
        </property>
    </bean>
</property>

However, if your field mappings are some kind of reusable DSL, you may try to think about implementing a namespace extension.

axtavt