Saturday, 13 May 2017

Story Of IOC ☺




QUICK EXAMPLE:Inversion of Control is about getting freedom, more flexibility, and less dependency. When you are using a desktop computer, you are slaved (or say, controlled). You have to sit before a screen and look at it. Using keyboard to type and using mouse to navigate. And a bad written software can slave you even more. If you replaced your desktop with a laptop, then you somewhat inverted control. You can easily take it and move around. So now you can control where you are with your computer, instead of computer controlling it.


IOC and DI


For example : Suppose we have an object Employee and it has a dependency on object Address. So we define a bean corresponding to Employee where it will define its dependency on object Address. When Spring try to create an Object Employee it sees that Employee has a dependency on object Address so first it will create the Address object (dependent object) and then inject this into the Employee Object.
  • Inversion of Control (IOC) and Dependency Injection (DI) are used interchangeably. IOC is achieved through DI. DI is the process of providing the dependencies and IOC is the end result of DI (Note: DI is not the only way to achieve IOC, there are other ways as well).
  • By DI the responsibility of creating objects is shifted from our application code to Spring container hence the phenomenon is called IOC.
  • Dependency Injection can be done by setter injection, constructor injection.

What are the Differences between  @annotations ? 😕

First the Similarity
First point worth highlighting again is that with respect to scan-auto-detection and dependency injection for BeanDefinition all these annotation (viz., @Component, @Service, @Repository, @Controller) are all the same. You can use one in place of another and can still get your way around.

Differences between @Component, @Repository, @Controller and @Service

@Component
This is a general-purpose stereotype annotation indicating that the class is spring component.
What’s special about @Component
<context:component-scan> only scans @Component and do not looks for @Controller@Serviceand @Repository in general. They are scanned because they themselves are annotated with @Component.
Just take a look at @Controller@Service and @Repository annotation definition
@Component
public @interface Service {
    ….
}

@Component
public @interface Repository {
    ….
}

@Component
public @interface Controller {
    
}
Thus it’s not wrong to say that @Controller@Service and @Repository are special type of @Component annotation. <context:component-scan> picks them up and registers their following classes as beans, just as if they were annotated with @Component.
They are scanned because they themselves are annotated with @Component annotation. If you define your own custom annotation and annotate it with @Component, then it will also get scanned with <context:component-scan>

@Repository
This is to indicate that the class defines a data repository.
What’s special about @Repository?
In addition to point out that this is an Annotation based Configuration@Repository’s job is to catch Platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. And for this, we’re provided with PersistenceExceptionTranslationPostProcessor, that we’re required to add in our Spring’s application context like this:
<bean class=”org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor”/>
This bean post processor adds an advisor to any bean that’s annotated with @Repository so that any platform specific exceptions are caught and then rethrown as one of Spring’s unchecked data access exceptions.

@Controller
The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role.
What’s special about @Controller?
You cannot switch this annotation with any other like @Service or @Repository, even though they look same. The dispatcher scans the classes annotated with @Controller and detects @RequestMapping annotations within them. You can only use @RequestMapping on @Controllerannotated classes.

@Service
@Services hold business logic and call method in repository layer.
What’s special about @Service?
Apart from the fact that it is used to indicate that it's holding the business logic, there’s no noticeable specialty that this annotation provides, but who knows, spring may add some additional exceptional in future.

@Component - Annotate your other components (for example REST resource classes) with component stereotype.
Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.
enter image description here



Which is better constructor injection or setter injection?


  1. Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.
  2. Overriding: Setter injection overrides the constructor injection. If we use both constructor and setter injection, IOC container will use the setter injection.
  3. Changes: We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection