|
It is a season which starts after winter and ends before summer.
It is a Java framework which takes care of managing the objects by creating some objects for you
to use without worrying about objects management. Along with that it gives many ready to use
functionalities.
Spring Framework is a Java-based framework that helps developers build robust, scalable, and
loosely coupled applications easily. It does so with the help of IOC (Inversion of control) by
DI (Dependency Injection).
IOC (Inversion of Control)
It is a design principle where the control of object creation and dependency management is
inverted to some other application. In this case it is spring framework. It inverts the control.
In normal case one has to create an object:
public class Human{
private Eye eye;
public Human(){
this.eye = new Eye();
}
}
but in case of spring, it does this for you.
@Component
public class Human{
private Eye eye;
@Autowired
public Human(Eye eye){
this.eye = eye;
}
}
Dependency Injection
It is design pattern used to remove tight coupling. That is, rather than classes creating objects of different
classes, some other great power(Spring) will inject that dependency. There are 3 ways of Dependency Injections.
Constructor Injection:
@Component
public class Human{
private Eye eye;
@Autowired
public Human(Eye eye){
this.eye = eye;
}
}
Setter Injection:
@Component
public class Human{
private Eye eye;
@Autowired
public setEye(Eye eye){
this.eye = eye;
}
}
Field Injection:
@Component
public class Human{
@Autowired
private Eye eye;
}
There are a lot of things Spring can do, these are just the ones which makes it so important.
|