when to use Setter Injection & constructor Injection in Spring?

Setter Injection-i am explaning through example hope users like this explaination
Suppose we have Class Hello and Class Hello having 4 property  and inside the xml document we intilaize only 2 property  int x and String str so when we we start the spring container only two property will be intialize
int x=100;
String str="Hello Guys";

And rest property int y ,and String msg will not be intialize but Spring container will not through any exception in that case.so in the case of Setter Injection if our application is very Huge and Having 1000 property and if developer forget to intialize  the property  inside the Xml Document  than Spring Container will not through any exception and we will not know the which Bean will not intialize in the case of Setter Injection.

public class Hello {
                          private int x;
                          private String str;
                          private int y;
                          private String msg;
                          Hello()
                           {
System.out.println("Hello-D.C");
                          }
                          public void setX(int x)
                         {
this.x=x;
                           }
                         public void setStr(String str)
                          {
                         this.str=str;
                         }

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

<bean id="hello" class="com.spring.Hello" lazy-init="true">
<property name="x" value="100"/>
<property name="str">
<value>Hello Guys</value>
</property></bean>
</bean>

Constructor Injection--

Constructor-injection automatically enforces the order .and Constructor-injection try to find the exact matching aruguments.If you write the 2-arguments Constructor and inside the Xml documnet you write only 1-argument like 
<constructor-arg value="200"/>

In that case when you start Spring Container and Container try to find the exact matching arguments and if will not find that exact matching it will through the Exception.so in the case of Constructor-injection exact matching is must.but in the case of Setter-injection is not must .

Constructor-injection automatically enforces the order.Suppose we have 2-arguments Construcor 

  public Hai(String str,String msg)
Both arguments are String type.if we change the order of both arguments than Spring Container dosent know abt it.and inject the dependency.and we will get the wrong data.so in the case of Contructor-injection order should be must.

public class Hai {
private int a;
private String msg;
public Hai()
{
System.out.println("Hai-D.C");
}
public Hai(int a,String msg)
{
System.out.println("Hai-2 arg con...");
this.a=a;
this.msg=msg;
}

<bean id="hai" class="com.spring.Hai">
<constructor-arg value="200"/>
<constructor-arg>
<value>Hai Guys</value>
</constructor-arg></bean>

3 comments :

  1. Is There basically only two types of injections are known???

    ReplyDelete
  2. In think one must use Setter only for optional dependency as mentioned on How to choose between Setter vs Constructor dependency Injection

    ReplyDelete
  3. what type of exception will throw in the case of constructor injection, when exact matching arguments ....

    ReplyDelete