Gordon Dickens is an instructor, mentor & consultant. Gordon is currently architecting and teaching several official SpringSource courses and actively tweets about open source technology at http://twitter.com/gdickens. Gordon is active within the Spring Framework community focussed on: Spring training, Spring Roo, Spring Integration, Spring Batch and Eclipse Virgo OSGi projects. Gordon is a DZone MVB and is not an employee of DZone and has posted 39 posts at DZone. You can read more from them at their website. View Full User Profile

Spring 3.1 Constructor Namespace

06.23.2012
| 6337 views |
  • submit to reddit

Spring provides several namespaces to simplify XML configuration, such as jdbc, tx, aop, etc. We Spring developers are already familiar with the required beans namespace.

<beans>
  <bean id="dataSource" class="..."/>
  <bean id="messagingProvider" class="..."/>
</beans>

Spring Namespaces are defined at the top of the XML file. Here we define the namespaces we need to use, and reference the schema (XSD) that validates the XML.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
 
http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans.xsd
 
http://www.springframework.org/schema/context
 
    http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.gordondickens"/>
 
    <bean id="simpleBean"
      class="com.gordondickens.springthreeone.SimpleBean"/>
 
</beans>

In Spring XML configuration we declare dependency injection with either “setter injection” (via properties tag) or “constructor injection”. Spring already provided the Property or “p” namespace, to simplify property setting, in other words, invoking setters on a bean.

Here we have a simple bean that can be configured with either a constructor or setter.

public class SimpleBean {
  private String myString;
  private String myOtherString;
 
  public SimpleBean() {}
 
  public SimpleBean(String myString) {
    this.myString = myString;
  }
 
  public SimpleBean(String myString, String other) {
    this.myString = myString;
    this.myOtherString = other;
  }
 
  public String getMyString() {
    return myString;
  }
 
  public String getMyOtherString() {
    return myOtherString;
  }
}

Instead of

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean id="setterBean"
      class="com.gordondickens.springthreeone.SimpleBean">
        <property name="myString" value="Setter Set"/>
    </bean>
 
</beans>

We could use

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="setterBean"
    class="com.gordondickens.springthreeone.SimpleBean"
      p:myString="Setter Set"/>
 
</beans>

With the Property namespace, there is no significant benefit, it is simple a matter of preference.

Spring Constructor Injection

Some developers choose to use “constructor injection” for several reasons.

  • Setting required elements
  • Immutable classes for thread safety
  • Third party class that only provides constructor setting

One of the main issues with “constructor injection”, prior to Spring 3.1, is that it is unclear which arguments are being set in the constructor injection. Also, if the constructor has multiple arguments, one can inadvertently set arguments in the wrong sequence.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="setterBean" class="com.gordondickens.springthreeone.SimpleBean">
    <constructor-arg value="squibbity doo"/>
    <constructor-arg value="flabbity doo"/>
  </bean>
</beans>

Spring 3.1 Constructor “c” Namespace

Unlike the “p” namespace, the “c” namespace provides us with descriptive references, allowing us to reference the constructor’s arguments by name. This feature is even more useful when we have multiple constructor arguments, as below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:c="http://www.springframework.org/schema/c"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="setterBean" class="com.gordondickens.springthreeone.SimpleBean"
    c:myString="squibbity doo"
    c:myOtherString="flabbity doo"/>
 
</beans>

Deployment Requirements

NOTE: Code using the “c” namespace, MUST be deployed with Java Debugging Tables (the -g option)!

Summary

We Spring developers have shortcuts at our disposal to make configuration easier or less terse. The new “c” namespace provides us the benefit of named variables that we know from using the “p” namespace or <property … />. We should strive to make our beans immutable for thread safety. By using the “c” namespace get the developers lean towards property setting for readability.

Notice that both the “p” ad “c” namespaces do not have a schema reference in the XML header.

 

 

 

 

 

 

 

 

Published at DZone with permission of Gordon Dickens, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)