What is IOC in spring boot?

IOC stands for inversion of control.
The approach of outsourcing the construction and management of objects.

Spring container’s primary functions

  • create and manage objects is (Inversion of control)
  • inject object dependencies is (Dependency injection)


Configuring spring container

  • XML configuration file (legacy)
  • Java Annotation (modern)
  • Java Source code (modern)

Dependency injection

The dependency inversion principle.
The client delegates to another object the responsibility of providing its dependencies.

Injection Type

There are three types of injection with spring

  1. Field Injection (Not recommended)
  2. Setter Injection
  3. Constructor Injection

Injection type which one to use

Constructor injection

  • Use this when you have required dependencies.
  • Generally recommended by the spring.io development team as the first choice.

Setter Injection

  1. Use this when you have optional dependencies.
  2. If dependencies are not provided then your app can provide reasonable default logic.

What is spring Autowiring

For dependency injection spring use Autowiing
Spring will look for the class that matches: by class or interface
Spring will inject it automatically.. hence it is Autowired

@Component Annotation

  1. @Component annotation marks the class as spring bean
  2. A Spring bean is a regular Java class that is managed by Spring.
  3. @Component annotation makes the bean available for the Dependency injection.

@Qualifier Annotation

@Qualifier annotation comes into the picture when one interface has multiple implementations

@Primary annotation

Resolving issues with multiple interface implementation
@Primry annotation is the alternate solution for the @Qualifier annotation
@Primary is a class level annotation is a class level annotation
@Qualifier has higher priority than the @Primary

Problem with @Primary
Only class can be marked as @Primary
If you have multiple @Primary then the app will throw an error

@Qualifer is recommended because it has higher priority and is more specific because it is @paramter level annotation we can adjust it in the code based on the requirement.

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *