Spring Framework: Best Programming Practices Part 4


This is the fourth part of Spring Best Practices series as per Best practices introduction post. This forth part is related with the best practices when using Spring’s XML configurations. You can refer the other four posts through this links… Part 1, Part 2, Part 3 and Part 5. I am expecting the corrections (if any)as well as the new best ways from my readers.

  • Avoid using spring’s ‘autowiring’

Spring can autowire dependencies through introspection of the bean classes so that you do not have to explicitly specify the bean properties or constructor arguments. Bean properties can be autowired either by property names or matching types. Constructor arguments can be autowired by matching types. You can even specify the autodetect autowiring mode, which lets Spring choose an appropriate mechanism. As an example, consider the following

   1: <bean id=”orderService” class=”com..spring.OrderService” autowire=”byName”/>

The property names of the OrderService class are used to match a bean instance in the container. Autowiring can potentially save some typing and reduce clutter. However, you should not use it in real-world projects because it sacrifices the explicitness and maintainability of the configurations. Many tutorials and presentations tout autowiring as a cool feature in Spring without mentioning this implication. In my opinion, like object-pooling in Spring, it is more a marketing feature. It seems like a good idea to make the XML configuration file smaller, but this will actually increase the complexity down the road, especially when you are working on a large project where many beans are defined. Spring allows you mix autowiring and explicit wiring, but the inconsistency will make the XML configurations even more confusing.

  • Use naming conventions

This is the same philosophy as for Java code. Using clear, descriptive, and consistent name conventions across the project is very helpful for developers to understand the XML configurations. For bean ID, for example, you can follow the Java class field name convention. The bean ID for an instance of OrderServiceDAO would be orderServiceDAO.For large projects; you can add the package name as the prefix of the bean ID.

  • Use shortcut forms

The shortcut form is less verbose, since it moves property values and references from child elements into attributes. For example, the following

   1: <bean id=”orderService” class=”com.spring.OrderService”>
   2:     <property name=”companyName”>
   3:         <value>valuemometum</value>
   4:     </property>
   5:     <constructor-arg>
   6:         <ref bean=”orderDAO”>
   7:     </constructor-arg>
   8: </bean>

Can be rewritten in the shortcut form as:

   1: <bean id=”orderService” class=”com.spring.OrderService”>
   2:     <property name=”companyName” value=” valuemometum “/>
   3:     <constructor-arg ref=”orderDAO”/>
   4: </bean>

The shortcut form has been available since version 1.2. Note that there is no shortcut form for <ref local=”…”>.The shortcut form not only saves you some typing, but also makes the XML configuration files less cluttered. It can noticeably improve readability when many beans are defined in a configuration file.

  • Prefer type over index for constructor argument matching

Spring allows you to use a zero-based index to solve the ambiguity problem when a constructor has more than one arguments of the same type, or value tags are used. For example, instead of:

   1: <bean id=”billingService” class=”com.spring.BillingService”>
   2:     <constructor-arg index=”0″ value=”valuemometum”/>
   3:     <constructor-arg index=”1″ value=”100″/>
   4: </bean>

It is better to use the type attribute like this

   1: <bean id=”billingService” class=”com.spring.BillingService”>
   2:     <constructor-arg type=”java.lang.String” value=”valuemometum”/>
   3:     <constructor-arg type=”intvalue=”100″/>
   4: </bean>

Using index is somewhat less verbose, but it is more error-prone and hard to read compared to using the type attribute. You should only use index when there is an ambiguity problem in the constructor arguments.

  • Reuse bean definitions, if possible

Spring offers an inheritance-like mechanism to reduce the duplication of configuration information and make the XML configuration simpler. A child bean definition can inherit configuration information from its parent bean, which essentially serves as a template for the child beans. This is a must-use feature for large projects. All you need to do is to specify abstract=true for the parent bean, and the parent reference in the child bean. For example

   1: <bean id=”abstractService” abstract=”trueclass=”com.spring.AbstractService”>
   2:     <property name=”companyName” value=”valuemometum”/>
   3: </bean>
   5: <bean id=”shippingService” parent=”abstractService” class=”com.spring.ShippingService”>
   6:     <property name=”shippedBy” value=”valuemometum”/>
   7: </bean>

The shippingService bean inherits the value valuemometum for the companyName property from the abstractService bean. Note that if you do not specify a class or factory method for a bean definition, the bean is implicitly abstract.

  • Prefer assembling bean definitions through ApplicationContext over imports

Like imports in Ant scripts, Spring import elements are useful for assembling modularized bean definitions. For example

   1: <beans>    
   2:     <import resource=”billingServices.xml”/>
   3:     <import resource=”shippingServices.xml”/>
   5:     <bean id=”orderService” class=”com.spring.OrderService”/>
   6: <beans>

However, instead of pre-assembling them in the XML configurations using imports, it is more flexible to configure them through the ApplicationContext. Using ApplicationContext also makes the XML configurations easy to manage. You can pass an array of bean definitions to the ApplicationContext constructor as follows:

   1: String[] serviceResources = {”orderServices.xml”, “billingServices.xml”,”shippingServices.xml”};
   2: ApplicationContext orderServiceContext = new ClassPathXmlApplicationContext(serviceResources);

Technorati Tags: , ,

5 thoughts on “Spring Framework: Best Programming Practices Part 4

    1. I agree with the comment, it doesn’t make sense, especially for applications that do not explicitly use ApplicationContext. This completely goes against everything Spring is trying to accomplish.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: