This tutorial gives you an introduction of Spring Framework.
1. What is Spring
Spring is the open-source application framework, it provides a comprehensive programming and configuration model for Java-based enterprise application. Nowadays, Spring has become more popular in the Java community because it provides the platform in which the developer can create high performing, easily testable and reusable code.
The beauty of the framework is that, in most of the times we don’t need to depend on Spring’s specific Classes and Interfaces. This is unlike other Frameworks, where they will force the Client Applications to depend on their propriety of implementations.
We can focus on application-level business logic, without unnecessary ties to specific deployment environments while the Spring framework handles transaction APIs, remote APIs, JMX APIs, JMS APIs…
Using Spring framework gives us many benefits:
- Use POJOs (Plain Old Java Object). It’s easier and faster to use dependency injection for injecting test data, and we do not need an enterprise container such as an application server but we should consider using a servlet container.
- Do not need to integrate the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers… because Spring truly makes use of them.
- Phasing Spring into an existing or new project can be done on case-by-case or module-by-module basis because Spring is modular, it consists of features which are organized into about 20 modules with more readable codes and loose coupling between them.
- Spring MVC is very flexible. When using Struts, you make your Action and Form objects by concrete inheritance, while Spring MVC is entirely based on interfaces, and every part is configurable via plugging in your own interface.
- Spring has a consistent transaction management interface that can scale down to a local transaction (resource-specific, such as a transaction associated with a JDBC connection) or scale up to global transactions (work with multiple transactional resources, typically relational databases and message queues).
- IoC container (Inversion of Control container – the core of the Spring Framework) helps you code to abstractions, loose couple between components by enforcing the Dependency Injection pattern, and it tends to be lightweight. So developing and deploying applications on computers with limited memory and CPU resources will be easier.
- Spring has API to translate technology-specific exceptions (for example, SQLException to its own exception class hierarchy with the DataAccessException as the root exception) into consistent, unchecked exceptions. These exceptions includes original exception inside, you don’t have any risk of losing any information or getting something wrong.
- Flexible use of Dependency Injection: can be configured by XML based schema or annotation-based style.
- Supports caching, validation and formatting.
3. Main features
a- Dependency Injection
To decouple Java components from others, the class shouldn’t create or find other objects, but inject them into, to make them dependent.
Dependency Injection (DI) is one example of concept ‘Inversion of Control’ – a class should not configure itself but should be configured from outside via:
– passing parameters to the constructor (construction injection)
– using a setter method (setter injection)
The design basing on independent classes or components will increases the re-usability and possibility of testing software, and Dependency Injection is the heart of Spring Framework, the technology that Spring is most identified with.
b- Aspect Oriented programming
Aspect-Oriented Programming (AOP) provides new way of thinking about program structure. Aspects enable transaction management which has ability of cutting across many methods, objects and types. You can see various examples of aspects: logging, auditing, declarative transactions, security, caching…
AOP is one of the key components of Spring Framework. DI helps you decouple your application objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect.
Spring AOP provides interceptors to intercept an application, for example, when a method is executed, you can add extra functionality before or after the method execution.
Spring AOP is implemented in pure Java, you don’t not need to control the class loader hierarchy, so it is suitable for using in a Servlet container or application server.
c- MVC Framework for Web Application and RESTful Web Service
You can develope a flexible and loosely couple web application by using Spring Web model-view-controller architecture and components:
– The Model includes application data. In general, it contains POJOs.
– The View is for rendering the model data. In general, it creates HTML output for client’s browser.
– The Controller processes user requests, builds corresponding model and passes it to The View for rendering.
Spring Web MVC has a DispatcherServlet that dispatches requests to handlers (with handler mappings), view resolution, locale, time zone and theme resolution as well as support for uploading files.
@Controller and @RequestMapping annotations provides flexible handling methods. @Controller also helps you create RESTful Web sites and applications by using @PathVariable annotation and other features.
d- Data Access
Spring Data provides a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.
It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. Spring Data is an umbrella project which contains many subprojects that are specific to a given database (JPA, MongoDB, Redis, Solr…)
The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules.
e- And more…
If you want an easier way to deal with JDBC, want a better way to work with your ORM, want to make use of one of the best Java MVC frameworks out there, want an easy way to provide and consume Web Services, both the heavy kind and the REST kind… Just be familiar with Spring.
Last updated on May 17, 2017.