Tuesday, March 4, 2014

spring constructor injection

Lets take two classes parent class and children class.
Parent class want's children object within itself.

Class Children {
public helloChildren (){
System.out.orintln ("hello children");
}
}


Class Parent {
private Children children;
@autowired
public Parent (Children children){
this.children=children;
}
public void helloParent (){
System.out.println ("hello parent");
children.helloChildren ();
}
}

Its done threw annotation. 

spring dependency injection

There are three type of dependency injection in spring:-

  1. Constructor Injection
  2. Setter Injection
  3. Interface Injection
If there is constructor injection, setter injection and interfeace injection in same class Constructor injection is prefered overriding others.

It there is setter injection and interface injection then setter injection is preferred over interface injection.