Spring basics

Hey folks!
Frankly, for me medium is more of a self-note than a medium to share knowledge, that’s maybe because there are always many good resources on hot frameworks such as Spring and it is not very justifiable to write one more, some would say!
Nevertheless, my simple way of putting across information maybe found useful by someone out there and that is enough motivation for me to continue scribbling :)
What is it that spring offers as a framework that makes the life of a developer easier?
At its core is built-in support for some of the best practices and low-level design choices that provides Java developers a sound platform to build on!
Let’s talk about couple of these features below-
Lightweight development with POJOs — Spring was proposed as a replacement of Java EE development with EJBs. EJBs are heavy objects, and spring proposed relatively lightweight and simple POJOs instead.
Dependency injection to promote loose coupling — the traditional way of object creation in java includes an object being created inside the constructor of its dependent object.
For example-
We can say that their is strong coupling between objects, in the sense that RequiredClass1 will only be created when DepedendentClass1’s constructor is called, but what if it is also required by an instance of DependentClassK ?
NOTE: RequiredClass1 is a base class, i.e. it is not dependent on any other class for its creation, instead other classes are dependent on it such as DependentClass1.
So, the logical alternative is to create objects with lesser or no dependencies first and then create dependent objects.
So, the control of creating a base object was earlier with the dependent object for eg. DependentClass1 controlled when RequiredClass1 gets created, i.e. it will not get created unless DependentClass1’s constructor is called,
but now we want to reverse this, and first create base objects and take away this control from dependent classes such as DependentClass1 on when a base object is getting created.
This is called Inversion of control. By Inversion of control, we mean inverting who controls when an object is getting created!!
Pre-Spring: Dependent class controlled when a base class’s object will get created.
Spring: Base class’s object will always be created first and since it is required to create the dependent class’s object, simply now Base class object’s controls when dependent class’s object will get created.
Advantages-
- It is simply logical and makes more sense.
- Reuse of objects — now a central registry keeps track of every object that has been created and it will inject that same object’s reference to each class where it is required.
- Object management no more a worry for the developer — spring will take care of instantiating all required objects at start of application and developer doesn’t need to worry about it beyond adding annotations like @Autowired to communicate to Spring framework which all objects need to be created and injected and where in code.
A brief note summarizing same-
Declarative programming with Aspect-Oriented programming(AOP) — Having standard addon packages for standard utilities such as Logging, Security, Transactions etc.
References:
[1] Spring Udemy course by Chad Darby