Why use Spring framework when we can achieve loosely coupled code using interfaces?

Multi tool use
Why use Spring framework when we can achieve loosely coupled code using interfaces?
As far as i know we use spring IOC so that it becomes easy for developer to check the xml file to identify and control the flow of code by providing the dependencies explicitly.
But if we use autowire, developer actually has to check the code to identify which implementation is autowired to given class
same thing could have been achieved without actually using spring framework for example, so why we need spring or what is the real advantage of using springs in this case.
Interface Vehicle {
.....
}
Class Car implements Vehicle {
......
}
Class Example {
Vehicle v = new Car()
}
Or
Class Example {
@Autowire
Vehicle v;
....
}
1 Answer
1
This is a very good question!. Now for answer in the best way I consider that a little bit of story can be helps.
Spring was born in the EJB era in which all was configured in xml and Spring originally was a java framework for provide IOC POJO based. In this era was totally normal configure our application in xml and spring was a good choice because we can benefits of IOC without the EJB complexity and in a vendor free manner, EJB is not totally application server free because some times the vendor implementation especially for vendor specific configuration brings the application to do not totally portable, spring was a successful framework because was a solution for almost all the problems of an EJB application. Then the success of framework brings the framework to grow and include other capabilities like data access, web integration and more other integration and capabilities.
In this years also JavaEE grows and in JavaEE 6 was born CDI Spring provide a xml, annotation, java config groovy, kotlin dsl bean configuration in order to achieve flexibility and give a comfortable programming and configuration model, think about all the people that come from JabaEE 6+ that usually use @Inject for provide DI. Now said all this DI capability are to discretion of the developer.
In my opinion use @Autowired is dangerous because this brings you to Spring for ever and many issue of DI can arrives in case of more that one bean are configured. To enforce the effort of spring team to give you confort and free choice of configuration starting from Spring 4.3.x you can do a think like
interface Vehicle {
}
class Car implements Vehicle {
}
class Example {
private final Vehicle vehicle;
Example(Vehicle vehicle) {
this.vehicle = vehicle;
}
}
@Configuration
class Config{
@Bean
public Car car(){
return new Car();
}
@Bean
public Example example(Vehicle car){
return new Example(car);
}
}
@RestController
class Enpoint{
private final Example example;
Enpoint(Example example) {
this.example = example;
}
the your business code now is totally free from spring even the constructor on the rest controller do not required @Autowired annotation, of course you have configure it.
On the end I can say that you should use Spring not because Spring provide IOC, this can be achived in a Plain Java manner, whith Google Juice, Spring and many other IOC framework but I suggest you to use Spring for all the related project that give you many capabilities production ready like, Spring web for blocking IO, Spring WebFlux for no-blockin IO, Spring Integration for EIP, Spring batch for Enterprise ready Batch, Spring Cloud for Cloud native application and more and more, the power of Spring to day is the flexibility and completeness, of course, the law of spider man is valid! from great powers we have great responsibilities.
Spring give you many possibility but then is the developer responsibility to choose the best. Annotation give you speed especially during the prototyping but then is too rigid.
I hope that this long reflection give you an answer of your doubts
I would also recommend the book "Expert One-on-One J2EE Development without EJB" by Rod Johnson, the Spring creator. Although the book is 15 years old, it explains why Spring was being created.
– Andrei Damian-Fekete
Jun 30 at 14:07
absolutely! great book
– Valerio Vaudi
Jun 30 at 14:11
Thanks Valerio for answer
– Nikhil Kamani
yesterday
of nothing I hope that the my answer can be useful for you
– Valerio Vaudi
13 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
we use spring IOC so that it becomes easy for developer to check the xml file to identify and control the flow of code by providing the dependencies explicitly: no, not at all. It's been years since Spring apps don't use XML anymore, and dependency injection has never beean about XML. Among a ton of other things, Spring indeed provides dependency injection which is useful mainly to make the code easily testable, and to make AOP possible. There are thousands of resources online about this. Read en.wikipedia.org/wiki/Dependency_injection for example.
– JB Nizet
Jun 30 at 8:44