Spring 3.1 Constructor Namespace
Join the DZone community and get the full member experience.
Join For FreeSpring 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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Deploying Smart Contract on Ethereum Blockchain
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
-
Reactive Programming
-
From CPU to Memory: Techniques for Tracking Resource Consumption Over Time
Comments