When I was your age, I made my own toys.
Sorry, that is a lie. My loving parents spoiled me with most toys on the old Christmas list. And using Spring is like that for web developers. It provides objects to classes, giving them any toy they desire.
When I was your age, I made my own toys.
Sorry, that is a lie. My loving parents spoiled me with most toys on the old Christmas list. And using Spring is like that for web developers. It provides objects to classes, giving them any toy they desire.
Spring simplifies Java development. Spring uses plain old Java objects (POJOS) that are loosely coupled and connected through a process called dependency injection. Dependency injection is the keystone of Spring's ability to simplify development and maintenance. It allows independent software components to separate.
A tightly coupled code is like a child (the class) having to make his or her own toys (the objects). And unfortunately, the child can only play with those toys. While this may do away with the mother having to teach the child about sharing, it's sort of cruel, just as it is in Java development.
A better scenario: the mother (Spring) implements dependency injection upon all her children. At startup, the child's toys are dynamically "injected”. The child doesn’t know where the toys come from, but he or she can play with the toys in whatever manner desired.
Dependencies can be injected through three prominent methods: Constructor, Setter, and Interface Injection. Constructor and Setter are the most popular in Spring applications. Constructor injection is setting of an object as an argument of a classes’ constructor. Setter Injection, on the other hand, is setting of an object after instantiation through a bean-style setter method.
At this point, if you know nothing about Spring, you’re probably wondering how this all ties together. How do classes get injected with objects? There are two distinct, yet overlapping, ways in which this can be done.
The first way is through a configuration file (typically an XML). The configuration file’s sole purpose is to wire together all the dependencies, allowing for decoupling and leaving the classes void of unnecessary and cumbersome code.
The second way is to inject the dependencies through annotation-based configurations. Annotation-based configurations are done in-line of the source with a simple @ statement. While it seems intuitive to distance the source of the injection from your actual application using a configuration file, there are times where you may wish to make the code more aware of the actual injection--opting for annotation-based configurations. The chosen method is dependent on the actual application. Often, the combination of the two is the preferred method.
To use our child example again, the mother (configuration file) allows her children (classes) to freely play with any number of toys (objects) that come from any number of toy manufacturers (classes or interfaces). The lovingly injecting mother takes care of the children's responsibility to implement the toy manufacturer.
We build classes for multiple applications. We need dependency injection to keep code from becoming cluttered with implementations. After all, kids do a lot more than just play with toys.
This blog comes on the heels of finishing the first four chapters of Spring in Action, 3rd Edition: a book that I would highly recommend to anyone hoping to learn Spring. And check out the wonders of the Spring 3 Scheduler.
READ MORE.