Spring c-namespace XML Configuration Shortcut

This tutorial shows you how to use the spring c-namespace. In the previous tutorial we saw how to use the p-namespace to configure bean definition. The c-namespace allows you to configure the constructor arguments with inline arguments rather than nested constructor-arg elements.

Simple Bean

We are using this CoffeeMachine class. This class has a constructor taking 2 arguments. In the following Spring XML configuration file, we are configuring this bean using the c-namespace.

package com.memorynotfound.spring;

public class CoffeeMachine {

    private String bean;
    private double amount;

    public CoffeeMachine(String bean, double amount) {
        this.bean = bean;
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "CoffeeMachine{" +
                "bean='" + bean + '\'' +
                ", amount=" + amount +
                '}';
    }
}

Spring c-namespace XML shortcut

First we create a CoffeeMachine using classical spring XML configuration. We provide 2 <constructor-arg> elements which correspond to the 2 appropriate constructor arguments of the CoffeeMachine bean. Next we create a CoffeeMachine using the c-namespace. We provide the 2 constructor arguments as inline attributes of the <bean/> element.

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <description>
        The c-namespace allows configuring the constructor arguments rather then nested constructor-arg elements
    </description>

    <bean id="coffeeMachine" class="com.memorynotfound.spring.CoffeeMachine">
        <constructor-arg name="bean" value="Java"/>
        <constructor-arg name="amount" value="2.45"/>
    </bean>

    <bean id="cCoffeeMachine" class="com.memorynotfound.spring.CoffeeMachine"
          c:bean="Grocery"
          c:amount="2.19" />

</beans>

Bootstrap Spring Application

We created two bean definitions earlier. The traditional coffeeMachine bean and the c-namespace cCoffeeMachine bean. We simply print the result to the console to show the output.

package com.memorynotfound.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Run {

    public static void main(String... args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"app-config.xml"});

        // get bean with property
        CoffeeMachine coffeeMachine = context.getBean("coffeeMachine", CoffeeMachine.class);
        System.out.println(coffeeMachine);

        // get bean with c-namespace
        CoffeeMachine cCoffeeMachine = context.getBean("cCoffeeMachine", CoffeeMachine.class);
        System.out.println(cCoffeeMachine);

    }
}

Demo

The previous code generates the following output:

CoffeeMachine{bean='Java', amount=2.45}
CoffeeMachine{bean='Grocery', amount=2.19}

References

Download

You may also like...