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;
                          public void setX(int x)
                         public void setStr(String str)


<?xml version="1.0" encoding="UTF-8"?>
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>

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()
public Hai(int a,String msg)
System.out.println("Hai-2 arg con...");

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


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

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

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