Spring Dependency Injection
What is Inversion of control (IOC)?
πWhere control of object is transferred to container or framework.
πIOC allows container to take control on flow of program.
πTo implement IoC, the dependency injection is used.
Spring Container
π The interface "ApplicationContext" represents the spring container.
π Spring container instantiate, configuring and assemble spring beans and manage their life cycle.
Spring bean
π An object which is managed by spring container is known as spring bean.
What is Dependency in spring framework?
public class Invoice {
String name;
Double amount;
List<String> items;
Vendor vendor;
TaxDetails taxDetails;
}
class Vendor {
}
interface TaxDetails {
}
π Invoice class has various instance variables (name, amount, items, vendor, and taxDetails).
π If we want to create object of Invoice then we need all instance variables.
π So instance of class depends on all instance variables inside the class.
π If we want to create object of Invoice then we need all instance variables.
π So instance of class depends on all instance variables inside the class.
Dependent bean
π Invoice is target bean and Vendor is dependent bean.
Spring container injects dependencies, if dependent variables data type is
- Primitive Type: (byte, short, int, long, float, double, boolean, char, String)
- Collection Type Dependency: (List, Set, Map, Properties)
- Reference Type: (variables created using interface or class)
Injection
π Inserting something into another thing.
π Process of inserting one object into another object is called injection.
Spring Dependency Injection?
π Spring container injects other objects into an object.
π Dependencies which are declared in the spring bean.
Implementing DI
- XML based.
- Java annotation based.
Autowiring
π It will automatically create objects and wire the dependencies.
π Only provide @Autowired on variable
Autowire conflict
π If more than one same type of bean available in container, then spring throw NoUniqueBeanDefinitionException. (Conflict beans)
π To resolve this conflict, need to tell spring that which bean we want to inject using @Primary or @Qualifier annotations along with @Autowired.
Type of Spring Dependency Injection
π Setter Based Injection:
Spring container inject value to variable using setter method.
@Component
public class Vendor{
int code;
String name;
}
public class Invoice{
private Vendor vendor;
@Autowired
public void setVendor(Vendor vendor){
this.vendor=vendor;
}
}
Spring container inject value to variable using parameterized constructor.
@Component
public class Vendor{
int code;
String name;
}
public class Invoice{
private Vendor vendor;
@Autowired
public Invoice(Vendor vendor){
this.vendor=vendor;
}
}
When the annotation @Autowired is used on top of the field or property in the class, it is known as Field-based Dependency Injection
@Component
public class Vendor{
int code;
String name;
}
public class Invoice{
@Autowired
private Vendor vendor;
}
@Autowired(reuired=false)
π In this case we use @Autowired(reuired=false) to tell container that this dependency is optional.
π Setter Injection is preferred when bean properties of spring bean class are optional to participate in DI.
When To Use Setter Injection and When to Use Constructor Injection?
π Constructor injection is preferred when all the properties of bean class are required to participate in DI.π Setter Injection is preferred when bean properties of spring bean class are optional to participate in DI.
Comments
Post a Comment