See, when we are explaining Spring Framework to newcomers, we would say we want to avoid new object instantiation in code and Spring will inject dependencies. Thereby we will explain IoC and DI.
My question is:
"Why do we want Spring instantiation compared to traditional instantiation?"
Is the answer better object management? Or simply to take advantage of IoC and DI? Please list down concrete answer with explanations. I am wondering what might be the reason.
I think spring is better than direct object instantiation because with spring configuration is clearly separated from the other code. This makes the components loosely coupled. Due to loose coupling it is very easy to replace/modify one component without affecting others. This makes the code maintainable and more importantly testable leading to better quality software.
IMHO, Springs dependency injection supports and encourages use of good programming practices, like using interfaces (by-type injection) and modularizing things to different layers instead of writing huge classes (if you've ever seen "good old everything in Servlet"-webapps, you probably know what I mean). Since injecting a service- or DAO-object can be done easily, like with just a simple annotation (like @Autowired) or via XML-configuration, modularizing applications is fast and easy.
I'd stay it's still not useful to handle instantiation of everything via Spring, you can still create objects with new, if the objects are such that Spring doesn't need to be aware of them or you just use them somewhere for moving data between layers etc.
Some people have complained about the xml-configuration with Spring. With Spring 3.0, the new annotations make coding and simple configurations very fast, and your XML-configuration file needs only to contain the actual configuration of the software, not all the injections and "configuration" that actually never changes.. as an example, a few years back, we built a relatively simple in-house application with Spring 2.5 (it did have some annotations already, but we didn't use them), and the size of xml-configuration files grew to over 1000 lines in total very fast. Currently, we have a project with Spring 3.0 and the XML file contains only 100 lines, the stuff that actually needs to be configurable after building the project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With