DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Find out today at 11 am ET in our "DZone 2023 Preview: DevOps Edition!"
Last chance to join
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring 3.1 Constructor Namespace

Spring 3.1 Constructor Namespace

Gordon Dickens user avatar by
Gordon Dickens
·
Jun. 24, 12 · Interview
Like (1)
Save
Tweet
Share
24.83K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

 

 

 

 

 

 

 

Spring Framework

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Memory Debugging: A Deep Level of Insight
  • Beginners’ Guide to Run a Linux Server Securely
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Getting Started With JMS-ActiveMQ: Explained in a Simple Way

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: