What are the different way of injection bean properties In Spring Framework

In this post, we will learn, the different way of injection Bean properties in spring framework. Bean may contain the properties of the following types:

  1. Simple Types(Primitives, Wrappers, Strings and Dates)
  2. List of simple types
  3. Set of simple types
  4. Map of simple types
  5. Properties of simple types
  6. Other Bean types
  7. Collection of other Bean types

The above-said properties can be injected using setter injection or constructor injection.

1. Simple types: 
public class Customer {
int cid;
String cname;
Long phone;
}

Constructor Injection

Setter Injection

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-args value=”101”/>
<constructor-args value=”Ashish”/>
<constructor-args value=”9999”/>
</bean>
<bean id=”cust1” class=”com.xadmin.spring.customer”>
<property name=”cid” value=”102”/>
<property name=”cname” value=”Bhagat”/>
<property name=”phone” value=”998811334”/>
</bean>

2. List of simple types:
public class Customer {
List<String> emails;
}

Constructor Injection

Setter Injection

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg>
<list>
<value>aashish2012@gmail.com</value>
<value>aashish@gmail.com</value>
</list>
<constructor-arg>
</bean>

 

<bean id=”cust1” class=”com.xadmin.spring.customer”>
<property name=”emails”> 
<list>
<value>aashish2012@gmail.com</value>
<value>aashish@gmail.com</value>
</list>
</property>
</bean>

3. Set of Simple types:
public class Customer {
Set<String> emails;
}

Constructor Injection

Setter Injection

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg>
<set>
<value>aashish2012@gmail.com</value>
<value>aashish@gmail.com</value>
</set>
<constructor-arg>
</bean>

 

<bean id=”cust1” class=”com.xadmin.spring.customer”>
<property name=”emails”> 
<set>
<value>aashish2012@gmail.com</value>
<value>aashish@gmail.com</value>
</set>
</property>
</bean>

 

4. Map of Simple types:
public class Customer {
Map<String, long> refs;
}

Constructor Injection

Setter Injection

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg>
<map>
<entry key=”AA” value=”11”/>
<entry key=”BB” value=”22”/>
</map>
<constructor-arg>
</bean>

 

<bean id=”cust1” class=”com.xadmin.spring.customer”>
<property name=”refs”> 
<map>
<entry key=”AA” value=”11”/>
<entry key=”BB” value=”22”/>
</map>
</property>
</bean>

 

5. Properties of Simple types:
public class Customer {
Properties myprops;
}

Constructor Injection

Setter Injection

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg>
<map>
<entry key=”AA” value=”11”/>
<entry key=”BB” value=”22”/>
</map>
<constructor-arg>
</bean>

 

<bean id=”cust1” class=”com.xadmin.spring.customer”>
<property name=”myprops”> 
<props>
<prop><entry key=”AA” >11</prop>
<prop><entry key=”BB”>22</prop>
</props>
</property>
</bean>

 

6. Other Bean types:
public class Customer {
Address address;
}

Constructor Injection

Setter Injection

<bean id=”add” class=”com.xadmin.spring.Address”>
<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg ref=”add”/>
</bean>

 

<bean id=”add” class=”com.xadmin.spring.Address”>
<bean id=”cust” class=”com.xadmin.spring.customer”>
<property name=”address” ref=add”/>
</bean>

 

7. Collection of other Bean types:
public class Customer {
List<Address>accounts;
}

Constructor Injection

Setter Injection

<bean id=”add1” class=”com.xadmin.spring.Address”>
<bean id=”add2” class=”com.xadmin.spring.Address”>

<bean id=”cust” class=”com.xadmin.spring.customer”>
<constructor-arg>
<list>
<ref bean=”add1”/>
<ref bean=”add2”/>
</list>
</constructor-arg>
</bean>

 

<bean id=”add1” class=”com.xadmin.spring.Address”>
<bean id=”add2” class=”com.xadmin.spring.Address”>

<bean id=”cust” class=”com.xadmin.spring.customer”>
<property name=”addresses”>
<list>
<ref bean=”add1”/>
<ref bean=”add2”/>
</list>
</property>
</bean>

 

Example: Program for the injection of bean properties in spring.
File Required:

  1. Xadmin.java
  2. Account.java
  3. Address.java
  4. Customer.java
  5. Xadmin.xml

Xadmin.java

Package com.xadmin.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXml.ApplicationContext
public class xadmin
{
public static void main( String args[])
{
ApplicationContext ctx= new ClassPathXmlApplicationContext(“xadmin.xml”);
Customer cust= (Customer)ctx.getBean(“cust”);
cust.show();
}
}

Account.java

package com.xadmin.spring;
public class Account
{
private int accno;
private string atype;
private double bal;
private Account(){
System.out.println(“Account-D.C”);
}
// Setter and Getters
Public String toString()
{
return “”+accno+”\t”+atype=”\t”+bal;
}
}

Address.java

package com.xadmin.spring;
public class Account
{
private string city;
private string state;
public Address(string city, string street, string state)
{
System.out.println(“Address-3-arg”);
this.city=city;
this.state=state;
this.street=street;
}
public string toString()
{
return “”+city+”\t”+street+”\t”+state;
}
}

Customer.java

package com.xadmin.spring;
import java.util.*;

public class Customer
{
private int cid;
private string cname;
private string email;
private long phone;
private List<String> emails;
private List<Long> phones;
private Map<String, Long> refs;
private Properties myprops;
private Address address
private List<Account> accounts;
 public customer(int cid, string cname, string email, long phone)
{
System.out.println(“customer 4 args”);
this.cid=cid;
this.cname=cname;
this.email=email;
this.phone=phone;
}
//setter and getters

public void show()
{
System.out.println(cid);
System.out.println(cname);
System.out.println(email);
System.out.println(phone);
System.out.println(emails);
System.out.println(phones);
System.out.println(refs);
System.out.println(myprops);
System.out.println(address);

for(Account acc=accounts)
{
System.out.println(acc);
}
System.out.println(emails.getClass().getName());
System.out.println(phones.getClass().getName());
System.out.println(refs.getClass().getName());
System.out.println(myprops.getClass().getName());
}
}

xadmin.xml

<beans>
<bean id=”add” class=”com.xadmin.spring.Address”>
<constructor-arg value=”SouthDelhi”/>
<constructor-arg value=”MalviyaNagar”/>
<constructor-arg value=”Delhi”/>
<bean>

<bean id=”acc1” class=”com.xadmin.spring.Account”>
<property name=”accno” value=”11”/>
<property name=”atype” value=”SA”/>
<property name=”bal” value=”1000.00”/>
</bean>

<bean id=”acc2” class=”com.xadmin.spring.Account”>
<property name=”accno” value=”12”/>
<property name=”atype” value=”SA”/>
<property name=”bal” value=”2000.00”/>
</bean>

<bean id=”acc3” class=”com.xadmin.spring.Account”>
<property name=”accno” value=”13”/>
<property name=”atype” value=”CA”/>
<property name=”bal” value=”3000.00”/>
</bean>

<bean id=”cust” class=”com.xadmin.spring.Customer”>
<constructor-arg value=”101”/>
<constructor-arg value=”Ashish”/>
<constructor-arg value=”aashish2012@gmail.com”/>
<constructor-arg value=”9999”/>
<property name=”emails”>
<list>
<value>aa@jtc</value>
<value>bb@jtc</value>
<value>cc@jtc</value>
</list>
</property>
<property name=”phones”>
<set>
<value>111</value>
<value>222</value>
<value>333</value>
</set>
</property>
<property name=”refs”>
<map>
<entery key=”AA” value=”11”/>
<entery key=”BB” value=”11”/>
<entery key=”CC” value=”33”/>
</map>
</property>

<property name=”myprops”>
<props>
<prop key=”AA”>11</prop>
<prop key=”BB”>22</prop>
<prop key=”CC”>33</prop>
</props>
</property>


<property name=”address” ref=”add”/>
<property name=”accounts”>
<list>
<ref bean=”acc1”/>
<ref bean=”acc2”/>
<ref bean=”acc3”/>
</list>
</property>
<bean>
<beans>

Conclusion:
This tutorial gave you an applied approach for different injection of bean property the Spring framework. Both possible ways, with annotations and with XML were discussed. Please feel free to comment in the comment box below, if you have any doubt regarding the topic or you want to share more information about the topic. Happy Learning!

 

Leave a Reply

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