SpringSpring Core

What is Spring Framework

Spring Framework is a Lightweight, Open source Application Container that supplies many features such as Dependency injection, AOP(Aspect Oriented Programming), Transaction management and much more to build a loosely coupled, Lightweight, Simple and Easily Testable java application.

Note: Don’t worry if you didn’t understand the above description because of the terms like Application Container, Dependency Injection and AOP as I have explained them below and in my next post.

By using Spring you can develop simple standalone java application, Web application, Mobile application, Data access application using JDBC or Hibernate, Android application and much more.

It was created in 2002 mainly as an alternative of heavier EJB(Enterprise Java Bean). it encourages POJO/Bean class development and simplifies the complexity of enterprise application development and makes it possible to use POJO/JavaBean to achieve things that were previously only possible with EJB.

Spring handles the infrastructure so you can focus on your application and simplifies our java development.

Advantages of Spring Framework

Let’s understand its key features which simplify the development of using Spring Framework.

  1. Loosely Coupled: It uses the Dependency Injection design pattern to make the application loosely coupled. Covering DI in a single post is out of scope so I covered it in my next post in detail.
  2. Lightweight and Simple: As mentioned earlier spring encourages POJO/Bean class-based development and never forces our POJO/Bean class to extend one of their classes or implement one of their interfaces, Unlike EJB  who forces us to extend and implement their classes, thus making our POJO Lightweight. Spring avoids (as much as possible) littering your application code with its API(Spring Code).Spring almost never forces you to implement a Spring-specific interface or extend a Spring-specific class. Instead, the classes in a Spring-based application often have no indication that they’re being used by Spring.                                           POJO/BEAN in spring would be like this:
    public class HelloWorldBean {
    public String sayHello() {
    return "Hello World";
    }
    }

    And POJO/BEAN in EJB would be like this:
    public class HelloWorldBean extends XyzClass implement abcInterface {
    public String sayHello() {
    return "Hello World";
    }
    } 
  3. No Boilerplate Code(Repetitive Code): In a typical application one has to write exception handling, connection closing, resource freeing, closing the stream and much more boilerplate or you can say more obvious and repetitive code.Spring eliminates all these repetitive codes so that you can focus on core business logic.Consider an example of an application which saves the data into a  database. Code without spring 
    try{
    open connection
    }
    catch(IOException){e.printStackTrace}try{
    The logic for saving values
    }
    catch(IOException){e.printStackTrace}try{
    closing connection
    }catch(Exception){e.printStackTrace}
    

    Code is written using Spring Framework
    Logic for saving values
    
       You can easily do the comparison, the number of steps has reduced drastically as spring has eliminated the boilerplate code like exception handling and closing connection.So We don’t have to handle these tasks as Spring is taking care of it implicitly.
  4. Testing: In the past, if you have done testing of your Java application using any testing framework like JUnit, TestNG you must have had a tough time testing the tightly coupled code.An application written using Spring Framework is totally loosely coupled hence it’s very easy to test our code.
  5. AOP(Aspect-oriented programming): Aspect Oriented Programming is the next exciting feature of the Spring after Dependency Injection.Covering AOP in a single post is not possible, I have covered it separately here in more detail.Let me explain you an overview what exactly AOP is.

Consider an OOP based


Class StudentController{

booolean saveStudentDetails(Student student) {
if(isUserLoggedIn()||isadmin()){
//logic for saving the student details
}
}
booolean deleteStudentDetails(Student student) {
if(isUserLoggedIn()||isadmin()){
//logic for deleting the student details
}

}
}

OOP with AOP based code:


Class StudentController{

booolean saveStudentDetails(Student student) {

//only logic for saving the student details

}
booolean deleteStudentDetails(Student student) {

//only logic for deleting the student details
}
}


AOP code written in a separate class which will run the isUserLoggedIn() and isadmin() before saving and deleting of the student details

If you compare the OOP code with AOP code, the if condition of checking whether the user is logged in or not and whether the user is admin or not is missing.The logic  now has been moved to a separate AOP based class which will run before saving and deleting of the student details.By doing all this we now get a better design.Because the checks like isAdmin(), isUserLoggedIn() is doesn’t fall under the logic of saving or deleting the student and is kind of repetitive in every method.Now with the help of AOP, we can only focus on writing our logic for saving and deleting student details.The other checks  like isAdmin(), isUserLoggedIn() will be implicitly run by Spring with the help of AOP.

One thought on “What is Spring Framework

Leave a Reply

Your email address will not be published. Required fields are marked *