Introduction to Spring Framework

Spring Framework is the most popular web application development framework for enterprise Java applications. Spring framework makes application development easier by promoting good programming practices using a POJO-based programming model. Spring framework was initially developed as a light weight alternative to J2EE application. It quickly became popular among Java developers. Now millions of Java developers use Spring Framework to create high performing, easily testable, and reusable code. We will see its magic working in coming tutorials.

What is difference between Core Java, JavaSE, JavaEE, J2EE or EJB, Spring? follow this link..

What are benefits of Spring Framework

  • Inversion of Control (IOC): It means dependent objects are not created by object/develoepr but are provided by the framework itself using configurations.
  • Dependency Injection (DI): It is a way of providing Inversion of Control in Spring Framework, It uses constructors or setter methods for creating required objects automatically using provided configurations.
  • Aspect Oriented Programming (AOP): It allows the separation of cross-cutting concerns (i.e. logging, auditing, declarative transactions, security, caching, etc) from the business logic.
  • Less boilerplate Java code: Spring has an extensive collection of various helper classes. These can be used to minimize the boilerplate code. it makes developement process easier.
  • Easy integrations with other frameworks/: Other frameworks and technologies such as Hibernate, ThymLeaf (Template Engine), Quartz Scheduler can very easily integrated into Spring.

Dependency Injection

Dependency (dependent objects), Injection (assgining the created object to the given variable). Dependent or tightly coupled code looks like below::

But in Spring it would be like below


As you can see, dependency is provided by Spring, rather than we creating it. Hence it is called Inversion of Control. Now control for injecting dependency is with Spring rather with developer.

Aspect Oriented Programming (AOP)

AOP addresses the problem of cross-cutting concerns, which would be any kind of code that is repeated in different methods and can’t normally be completely refactored into its own module, like with logging or verification. So, with AOP you can leave that stuff out of the main code and define it vertically like so. See the below code (Example taken from StackOverflow: ):


But it will be actually seen as below:

Architecture of Spring Framework

spring-modules Spring Framework Architecture

  • Core Container : The Core and Beans modules provide the most fundamental parts of the framework and provide the IoC and Dependency Injection features. The basic concept here is the BeanFactorywhich provides a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic
  • Spring Web module : Spring’s Web module provides basic web-oriented integration features, such as multipart file-upload functionality, the initialization of the IoC container using servlet listeners and a web-oriented application context. It also contains the web-related parts of Spring’s remoting support.
  • Data Access & Integration : The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and Transaction modules. We will see each terms in detailed in our course.
  • Spring AOP and Aspect : The Spring AOP module integrates Aspect Oriented Programming functionality directly into the Spring framework, which allows us to define segregate our cross-cutting functionalities from our business logic. As a result, we can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application.
  • Instrumentation : The Instrumentation module provides class instrumentation support and classloader implementations to be used in certain application servers.
  • Messaging : Spring framework’s messaging module serves as a foundation for messaging-based applications. The module provides a set of classes e.g., Message, MessageChannelMessageHandler to send and receive messages from the application and it also provides a set of annotations for mapping messages to methods, similar to the Spring MVC annotation based programming model.
  • Test : Spring framework’s Test module provides abstraction over a lot of testing and mocking framework such as TestNG, JUnit, Mockito, PowerMock which makes writing unit and integration a lot easier.