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.

Dependent bean

    πŸ’œ We define Invoice class as bean in spring and it has dependencies as vendor and taxDetails.
    πŸ’œ Invoice is target bean and Vendor is dependent bean.

Spring container injects dependencies, if dependent variables data type is
  1. Primitive Type: (byte, short, int, long, float, double, boolean, char, String)
  2. Collection Type Dependency: (List, Set, Map, Properties)
  3. 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
  1. XML based.
  2. Java annotation based.

Autowiring

    πŸ’œ Spring Container detects relationship between the beans at the time of booting up application.
    πŸ’œ 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;
    }
}

    πŸ’šConstructor Based Injection:
        
            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;
    }
}

    πŸ’š Field or property-based Injection
        
            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)

    πŸ’œ When bean is not registered with container and we apply autowire then it will throw exception for missing dependency.
    πŸ’œ In this case we use @Autowired(reuired=false) to tell container that this dependency is optional.

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

Popular posts from this blog

Welcome to Java study !!!

Java 8 Features