This quick Codemotion guide to Spring offers insights and key concepts for understanding and getting started with this popular framework.
A brief story of Spring
Today, Spring Framework is a de facto standard. Hundreds of thousands of developers around the world use it with great success.
Spring is an open-source tool created in 2003 to overcome a major flaw in the official standard and to offer a real lightweight component container solution. In October 2002, Rod Johnson’s book ‘Expert One-on-One J2EE Design and Development‘ was published. The book discussed the current state of Java enterprise application development and highlighted several serious deficiencies of Java EE and the EJB component framework. Johnson suggested a simpler solution focussed on ordinary Java classes and dependency injection. However, although some consider Java EE and Spring to be rivals, Spring is a backend modular framework that should be viewed as complementary to Java EE.
Since its inception, Spring continues to innovate and evolve with an active community offering continuous feedback.
Modular structure
The Spring framework contains 20 modules including Core, Beans, Context, Expression Language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, and Struts.
Spring’s modular framework gives it a certain facility for the design and creation of java E.E. applications. Today it integrates implementations of other technologies such as “.NET” or Python.
The basic modules of Spring (for the development of Java EE applications) are 20 in number and the effectiveness of Spring compared to other Java Frameworks is due to the fact that it uses Beans (simple POJO – they are not JavaBeans), configuration files (XML files) and annotations (from version 2.0 Spring annotations have been reinforced since version 2.5).
The Core Container is made up of the Core modules themselves, Beans, Context and EL (Expression Language):
- The Core module provides the core functionality of the framework in addition to IoC and Dependency injection.
- The Beans module offers the BeanFactory interface which allows an implementation of the Factory Method pattern.
- The Context module builds on the solid foundation provided by the Core and Beans modules and is a way to access objects defined and configured within an application developed with Spring. The ApplicationContext interface is the focal point of the Context module.
- The SpEL (Spring Expression Language) module provides a powerful expression language (derived from EL) that enables you to manipulate objects at runtime in a Spring application.
The Spring Framework uses different proven programming practices such as IoC (Inversion of Control), design patterns such as the Factory pattern, the Singleton pattern to name just these two. With the Factory pattern, Spring provides two types of IoC container implementation using the BeanFactory and ApplicationContext interfaces. The ApplicationContext interface derives from the BeanFactory interface which maintains their compatibility and makes the Bean configuration files of both implementations identical.
Spring MVC vs Spring Boot
Spring MVC is mainly used to develop web applications. Spring is a configuration-requiring backend framework, while Spring Boot is ready-to-use.
Spring Boot is based on all the default Spring features. Core Spring and MVC can handle the full functions of any Java application. Depending on the complexity and configuration, Spring Boot can help us a lot to reduce the complexity of Spring configuration.
Simply put, Spring Boot is an extension of the Spring framework, apart from the standard configurations required for setting up a Spring application.
It adopts a vision of Spring and has a faster and more efficient development ecosystem.
Here are some of the features of Spring Boot:
- Starter-like dependencies to simplify building and configuring the application
- Integrated server to avoid complexity when deploying applications
- Outsourced metrics, verification and configuration
- Auto Setup
Inversion of control container
IoC is a general design principle for programming and Dependency Injection (DI) is the concrete design pattern that embodies it.
Spring IoC is a very complete and complex framework. Its operation is based on 3 key elements:
Configuration by XML files to link implementations to their interfaces
Classes are beans and therefore have getters and setters for the fields to be injected
The injection is performed via a configuration file (by default it is ApplicationContext.xml) from Spring.
Aspect-Oriented Programming
As a modular framework, Spring supports the implementation of aspect-oriented programming where one can use Advices (concepts used in AOP), Pointcuts (concepts used in AOP) etc. to decouple the code.
The Aspects module provides support for integration with AspectJ.
The Instrumentation module supports class instrumentation and classloader implementations
Aspect-Oriented Programming (AOP) makes it possible to separate the business code and the technical code corresponding to cross-cutting concerns.
Data transactions
Spring Transaction is the specific module responsible for transaction integration. It offers several advantages:
It provides an abstraction over the different solutions available in the Java world for transaction management. Spring Transaction defines the TransactionManager interface to unify its different solutions and offers concrete classes for each of them.
It relies on Aspect-Oriented Programming (AOP) to manage transactional demarcation in our application code
It allows declarative management of transactions.
The Transaction
The notion of Transaction is recurrent in information systems. For example, most RDBMSs (Oracle, MySQL, PostgreSQL, etc.) include a transaction engine. A transaction is defined by respecting four properties designated by the acronym ACID:
Atomicity
The transaction guarantees that all the operations that make it up are either all carried out successfully or none are kept.
Consistency
The transaction guarantees that it moves the system from a valid state to another valid state.
Insulation
Two transactions are isolated from each other. That is to say that their simultaneous execution produces the same result as if they had been executed successively.
Durability
The transaction guarantees that after its execution, the changes it has made to the system are preserved durably.
A transaction is defined by a beginning and an end which can be either a validation of the modifications (commit) or a cancellation of the modifications made (rollback). We talk about transactional demarcation to designate the portion of code that must be executed as part of a transaction
First steps with Spring
There are plenty of resources to help you get to grips with the framework. Two books to understand the power of Spring are ‘Spring in Action‘ by Craig Walls and ‘Spring Recipes’ by Gary Mak et al.
Spring is easy to learn because the whole modular framework is designed to work with POJOs, instead of depending on special interfaces, abstract classes or such. Developers can write software as normal Java applications – interfaces, classes and enums and use Spring to wire the components up, without having to go out of the way to achieve the wiring.
Recommended article: Easily Migrate from Spring Boot to Micronaut