Categories
Spring Spring Core

Primitives as Dependency in Spring

In Spring Based application all the Objects are Created and Managed by IoC Container. For any Object we have dependency of another Object or Primitives in general. We are going to see a Detailed explanation of “How Primitives are used as Dependencies in Spring Framework?” in this Article.

Constructor-args

<constructor-arg index="..." name="..." ref="..." type="..." value="..."></constructor-arg>

The above code having all available attributes present in constructor-args. We shall see each and every attribute usage here. 

index
  • Can specify arguments with its index positions. 
  • Index starts with “0”.
  • Index value type should match the type of the constructor.(String to String, int to int and not like String to int).
  • It is an optional attribute.

Example : 

<bean id="employeeController" class="net.geekcoders.example.EmployeeController">
		<constructor-arg index="0" value="Kandha"></constructor-arg>
		<constructor-arg index="1" value="29"></constructor-arg>
		<constructor-arg index="3" value="174.6"></constructor-arg>
		<constructor-arg index="2" value="Developer"></constructor-arg>
	</bean>

Equivalent Controller class :

public EmployeeController(String empName,int empAge, String empRole, float empHeight) {
	// assignments of args goes here.	
}

name
  • Should provide matching arguments name as provided in constructor.
  • It’s an optional attribute.
<bean id="employeeController" class="net.geekcoders.example.EmployeeController">
		<constructor-arg name="empName" value="Kandha"></constructor-arg>
		<constructor-arg name="empAge" value="29"></constructor-arg>
		<constructor-arg name="empHeight" value="174.6"></constructor-arg>
		<constructor-arg index="empRole" value="Developer"></constructor-arg>
</bean>

ref

For Primitive dependencies you can omit the ref attribute.

type
  • Must Provide matching type as defined in constructor otherwise throws exception.
  • Ex., String, int, float,…
  • Spring automatically detects argument types and converts from String to String, String to int,… while injecting the dependencies.
  • type should be the last attribute in a list.
  • It is an optional attribute.
<bean id="employeeController" class="net.geekcoders.example.EmployeeController">
		<constructor-arg name="empName" value="Kandha" type="String"></constructor-arg>
		<constructor-arg name="empAge" value="29" type="int"></constructor-arg>
		<constructor-arg name="empHeight" value="174.6" type="float"></constructor-arg>
		<constructor-arg index="empRole" value="Developer" type="String"></constructor-arg>
	</bean>

value
  • You can provide any kind of values to the constructor
  • Ex., String, float, int, any object,…
  • It is Mandatory attribute 
  • You can provide empty String or Null value to the Value attribute.

Example 1 : Without Empty and Null value

<bean id="employeeController" class="net.geekcoders.example.EmployeeController">
		<constructor-arg name="empName" value="Kandha" type="String"></constructor-arg>
		<constructor-arg name="empAge" value="29" type="int"></constructor-arg>
		<constructor-arg name="empHeight" value="174.6" type="float"></constructor-arg>
		<constructor-arg index="empRole" value="Developer" type="String"></constructor-arg>
	</bean>

Example 2 : Passing Empty and Null value to String

<bean id="employeeController" class="net.geekcoders.example.EmployeeController">
		<constructor-arg name="empName" value="" type="String"></constructor-arg>
		<constructor-arg name="empAge" value="29" type="int"></constructor-arg>
		<constructor-arg name="empHeight" value="174.6" type="float"></constructor-arg>
		<constructor-arg index="empRole" type="String"><null/></constructor-arg>
	</bean>

Example 2 is equivalent to this.setEmpRole(null); and this.setEmpName(“”);

Property tag

<property name="..." value="..." ref=”...”></property>
name
  • Name should match with the setter method of class. 
  • It is a mandatory attribute
<property name="empName" value="Kandha"></property>

The above code is equivalent to this.setEmpName(“Kandha”);

value
  • Should provide correct value matching to the setter method of controller class.
  • If type mismatched throws exception
  • It is a mandatory attribute.
  • You can provide Empty String or Null value as like below.
<property name="empRole" value=""></property> // Empty String
<property name="test" ><null/></property> // null 

That’s all for now on Primitives as Dependencies in Spring

Thanks for Reading!!!

One reply on “Primitives as Dependency in Spring”

Leave a Reply

Your email address will not be published. Required fields are marked *