Getting Started With Spring Framework in less than 30 minutes
- In Programming
When you first hear the word “Spring” your mind creates a beautiful picture of a season which comes after winter and before summer. In this article, I’m going to inject a completely different image for the word Spring. I know you are not getting any bit of what I’m saying but till the end of this article you will have a clear understanding of these things and getting started with Spring Framework.
What is Spring Framework?
Let’s leave Spring for sometime and concentrate on the term “Software Framework”. According to the hardcore definition of the term “Software Framework” –
A software framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code, thus providing application-specific software.
To understand in the easiest of the terms, let’s try to relate it with the real world objects. Suppose, you have been given a task to create a single block of size 5X5 cm and you have been given a scale to measure that. It is a simple task for you, right? You will easily measure the size and create a block of required size. But now imagine a situation where you have to create 10000 different blocks of varying sizes.
Now, what’s your approach going to be? You cannot continue with your previous approach because that will take ages. A new and better solution would be to make a fixture of a fixed size which will save you time doing the same thing again and again, i.e, measuring (writing same code). If you have to make shapes with different sizes than you can add additional feature to your fixture so that it can measure variable size. This is what a framework do for you. You just have to adjust it according to your requirements and rest of the hardwork will be taken care by the framework itself.
Similarly, if we talk about the Spring framework, it follows a usual flow, a life-cycle. In this, you can hook your codes in between with the help of the Spring Lifecycle methods and the rest will be taken care by the spring. This will save you from writing the same code again and again and you will only left with the implementation or we can say the business logic of the application. In this way, Spring provides a more open yet structured framework using the dependency injection and Inversion of Control to decouple the dependencies between the modules. This enables us to easily integrate different pieces of technologies together to create a fast and scalable application.
Before getting started with Spring application, there are a few concepts that you must be versed with.
Spring container is at the core of this framework. Everything in Spring remains inside of a Spring container. Every object that is created in an application will live inside of this Spring container. Spring Container will be responsible for all the wiring between the object (act of creating associations between objects), there configuration and lifecycle management.
Basically, everything in Spring framework is a Bean. To make it very clear, bean is nothing special and it is just a convention for creating classes. A Java Bean is a java class that should follow following conventions:
– It should have a no-arg constructor.
– It should be Serializable.
-?It should provide methods to set and get the values of the properties, known as getter and setter methods.
Many developers will say that a bean is a POJO (Plain Old Java Object) class which is completely wrong. A bean is just like a container to hold and structure data which makes it easier to set and retrieve data. Bean has a lifecycle defined in Spring framework.
DI is a design pattern which is used by the developers to decouple different working modules in an efficient way. It is a way to provide the service whenever required by the client. It is used to pass the dependency to the dependent object whenever it is required by the client. The basic approach of this pattern is to provide the client with the required service at the time he needs it rather than him searching for it.
DI also enables programmers to follow the Inversion of Control flow for the application which further helps to decouple the dependencies between two modules.
Inversion of Control (IoC)
As the name of the principle says Inversion of Control simply means that the natural flow of the application is unversed in order to decouple two different modules from each other. This is done with the help of abstraction. To achieve this a custom written code is injected into the normal flow of the framework rather that calling different library funtions. This is what the “Hollywood Participle” says –
“Don’t Call Us, We Will Call You”
In this case, the user or the client does not call the library methods explicitly rather framework itself calls the client code in its lifecycle. This helps to achieve a greater level of modules decoupling and helps the client to only concentrate on the business logic of the application rather than its flow of execution. A simple example of IoC – Example. If your are not satisfied and want to dive deeper then refer to this article -?Inversion of Control and Dependency Injection.
First of all, you need all the required JARs before starting a project. You can find all the required JARs here… Simply download the folder and unzip anywhere on your pc from where you can easily access it from anywhere.
First thing that you need to do before getting started with Spring Framework is to download all the required JARs. You can follow the download link here. Once you have downloaded all the JARs then it’s time for setting up the environment. I’m going to use eclipse mars which you can download from this link here (1st link). Just follow the step by step guide for setting up your workspace.
Here is a step-by-step process for setting up your project in eclipse. Just follow the steps mentioned below.
- Create a Java Project?
- Give your project suitable name, select your execution environment and click finish.
- At this point your basic project has been created. Its time to add the required JARs into the build path of your current application. Simply download all the required JARs from this link which you will need in this project.
You have successfully setup your work environment. Now, you can start writing some code which will help you to better understand the working of Spring Framework.
This should be your file structure for this application. Now let’s write some code to make it work.
The most important thing is to understand the need of Spring Framework into your application. Once you know why you need a Spring Framework then you will find it easy to learn and it can save you hours of code. It is called a light-weight framework for JAVA. It is because you can get started with the minimum of the code. You do not need to use any of the Spring class or to extend it. You just plug your code into the Spring Framework and the rest is handled by the Spring itself. It can be used to build any Java application and makes application development bliss by providing powerful features like Dependency Injection (DI) and Inversion of Control (IoC).
Check out this article: Thinking in OOPs: Object Oriented Programming