Over a million developers have joined DZone.

Spring 3.1 Constructor Namespace

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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.

 

 

 

 

 

 

 

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Gordon Dickens, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}