Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
The c-namespace
in Spring provides a convenient way to configure bean properties by referring directly to constructor arguments, rather than traditional setter methods. The "c" stands for "constructor". It is particularly useful when you have beans with many constructor arguments or when you prefer constructor injection.
To use the c-namespace
, you'll need to include the namespace declaration in your XML configuration.
Setup:
Include the c-namespace
declaration in your Spring XML configuration:
<?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"> <!-- bean configurations go here --> </beans>
Example:
Suppose you have a Person
class like the following:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // getters, setters, and other methods... }
Now, instead of using the traditional constructor-arg
tags, you can use the c-namespace
:
<bean id="person" class="com.example.Person" c:name="John Doe" c:age="30"/>
Here, c:name
and c:age
correspond to the constructor parameters of the Person
class.
Points to Remember:
Ensure that your class has a matching constructor. If there's no matching constructor, you'll get a bean creation exception.
The c-namespace
provides a more concise way to define beans when using constructor-based dependency injection, but it doesn't replace the traditional method. You can choose whichever method you find more readable and convenient.
While the c-namespace
makes XML configurations more concise, many modern Spring applications are moving toward Java-based configurations, which provide type-safety and are more refactor-friendly.
Using c-namespace in Spring XML configuration:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example"> <property name="property1" value="Hello, c-namespace!" /> <property name="property2" value="42" /> </bean> </beans>
Spring c-namespace example project:
// Example.java package com.example; public class Example { private String property1; private int property2; // Getters and setters... @Override public String toString() { return "Example{property1='" + property1 + "', property2=" + property2 + '}'; } }
How to use c-namespace in Spring beans:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:property1="Hello, c-namespace!" c:property2="42" /> </beans>
Configuring properties with c-namespace in Spring:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:property1="Hello, c-namespace!" c:property2="42" /> </beans>
Spring c-namespace vs p-namespace:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- Using c-namespace for properties --> <bean id="exampleBeanC" class="com.example.Example" c:property1="Hello, c-namespace!" c:property2="42" /> <!-- Using p-namespace for constructor arguments --> <bean id="exampleBeanP" class="com.example.Example" p:property1="Hello, p-namespace!" p:property2="42" /> </beans>
Customizing Spring bean properties with c-namespace:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:property1="#{ systemProperties['user.name'] }" c:property2="#{ T(java.lang.Math).random() * 100 }" /> </beans>
c-namespace in Spring XML for constructor arguments:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:_0="Hello, constructor argument!" c:_1="42" /> </beans>
Working with c-namespace for property values in Spring:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:property1="Hello, c-namespace!" c:property2="42" /> </beans>
Spring c-namespace dynamic values example:
<!-- spring-config.xml --> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <bean id="exampleBean" class="com.example.Example" c:property1="#{ systemProperties['user.name'] }" c:property2="#{ T(java.lang.Math).random() * 100 }" /> </beans>