Why Spring framework

34 Flares Twitter 0 Facebook 7 Google+ 27 Pin It Share 0 LinkedIn 0 Filament.io 34 Flares ×

One of the hardest part in learning Spring is to understand why do you need spring and what is it exactly. Once this is clear, rest all becomes easy. Spring is generally described as ” A light weight framework for building Java applications”. This means any type of Java application – standalone java, JEE applications , Web applications etc. The light weight means you have to make only a few change or none at all to your application code to get the benefits of Spring. This is very important. You don’t need to extend your classes from any particular Spring framework component to get the framework features. This statement is applicable to Spring core.

Spring provides IOC  ( Inversion of Control) and DI ( Dependency Injection ) Capabilities. Minimizing the Dependencies is one of most important factors in creating maintainable, extensible software and Spring solves this beautifully. Let us explore IOC and Dependency Injection in the next section.

Inversion of Control ( IOC )

IoC is a technique that externalizes the creation and management of component dependencies. Inversion of Control is best understood through the term the “Hollywood Principle,” which basically means “Don’t call me, I’ll call you.” 

Consider the case where  Class Person depends on an instance of Class Address.  In traditional programming practice, Class Person either instantiates an instance of Address or Class Person uses a factory class to get the instance of Address. With the IOC practice, an instance of Address is given to Person at run time by the framework.  Thus the dependency is injected by the framework at run time and hence it is also called Dependency Injection. Effectively Inversion of Control is a means to achieve Dependency Injection. Many people use IOC and Dependency Injection interchangeably. In fact Spring acts like a container providing your application classes with all dependencies it needs.

Let us explore the beauty of Dependency Injection with an example

In the above Code, we simply tell spring that Person requires an instance of Address and that dependency should be satisfied using the setter method annotated with @Autowire.

@Autowired will instruct Spring framework to look for a bean that implements the required interface and inject it automatically via the setter.

The spring framework will  instantiate a valid instance of Address and wire it to the Person class. This is the beauty of Dependency Injection / IOC. There is no plumbing code put by the developer to satisfy the dependency. In my opinion IOC/ DI is the most important reason for the popularity and usefulness of Spring framework.  Spring framework provides better means of  non intrusive, plug-gable programming model.

There is more to Spring – it also provides declarative programming with AOP ( Aspect Oriented Programming). This is a better way to implement cross cutting concerns without the needs to use plumbing code all over the core business classes.

AOP enables you to think about concerns or aspects in your system. Typical concerns are transaction management, logging etc. AOP enables you to capture the cross-cutting code in modules such as interceptors that can be applied declaratively wherever the concern they express applies.Spring includes a proxy-based AOP framework.

But is that all that Spring offers ?. The answer is a BIG NO. Spring provides a lot of components that help with data access / integration, Web programming and other common programming requirements.

spring-overview

Spring also has a number of projects than utilizes the spring core and makes programming easier and cleaner.  Few of the Spring projects are

  • Spring Batch
  • Spring AMQP
  • Spring Boot
  • Spring Data Commons
  • Spring Data GemFire
  • Spring Data JDBC Extensions
  • Spring Data JDBC Extensions
  • Spring Data JPA
  • Spring Data MongoDB
  • Spring Data Neo4J
  • Spring Data Redis
  • Spring Data REST
  • Spring Data Solr
  • Spring Flex
  • Spring for Android
  • Spring for Apache Hadoop
  • Spring Framework
  • Spring HATEOAS
  • Spring Integration
  • Spring LDAP
  • Spring Mobile
  • Spring Roo
  • Spring Security
  • Spring Security OAuth
  • Spring Shell
  • Spring Social
  • Spring Social Facebook
  • Spring Social Twitter
  • Spring Web Flow
  • Spring Web Services

Do you need more reasons to switch to Spring framework.

  • RM

    Very good, I am going to start learning Spring. Your sharing does help me! Thanks

  • Raghavendra

    @Autowired will instruct Spring framework to look for a bean that implements the required interface and inject it automatically via the setter.

    A setter is not required to inject a dependency by Autowiring as it is injected by means of Reflection.