Spring incepted as an alternative to the EJB’s stringent rules of enterprise Java development with the flagship technique of dependency injection and AOP. This idea quickly garnered the interest of the developers who were already looking for an alternative solution. Today, Spring 4 has evolved into an umbrella of technology with a stack of technological features such as JDBC, ORM, Web Socket, Servlet, Portlet, Transaction, and so forth. The idea of convention rather than configuration is a major boost for a quick start-up for an enterprise solution with Spring BOOT. In a nutshell, in terms of feature richness, Spring seems to stay a step ahead in the middleware war of the core JEE support initiatives. This article delves into the framework from the perspective of one who wants to get an overall picture of the rabbit hole called Spring before diving deep into it.
An Overview of the Java Spring Framework
As an alternative to heavy enterprise Java technology, Spring offers a lighter and leaner programming model. Spring empowered POJO to participate in the enterprise programming context with functionalities akin to the specification provided by the EJB. J2EE’s EJB were a heavy enterprise component but over time it also evolved. Post JEE7, the core JEE platform also started offering a simple POJO-oriented programming model through EJB. Not only that, today EJB also supports DI and AOP. However, it should be kept in mind that Spring is basically an implementation of the EJB specification and is an alternative to its implementation. The playground of both Spring and EJB implementation (not specification) is the JEE arena. If one removes the specification from the scenario, both Spring and EJB will disappear. So, the conflict between the two is in their implementation and not in the specification. There are numerous EJB implementations, such as implemented by Application servers, such as JBoss, Glassfish, Wildfly, and so on.
Spring, as a Java platform for enterprise development, provides comprehensive infrastructural support both in terms of features, functionality, and ease of rapid development. With a strong conviction of incorporating nothing more than POJOs in the enterprise arena, Spring has revolutionised the development of enterprise solution. Pick a POJO and apply enterprise services with the annotation un-invasively. That is all what a POJO needs to designate as an enterprise component. Further, Spring encourages a lot of good object-oriented design principles such as MVC, dependency injection and AOP and the like. Also, Spring has a lot of extra features up its sleeve, such as spring-test, spring-BOOT, flexibility of incorporating any other framework, and so forth.
Spring Framework Modules
The core container consists of modules, such as spring-core, spring-beans, spring-context, spring-context-support, and spring-expression.
Figure 1: Spring framework (Ref. Spring Framework Reference Documentation).
- The spring-core and spring-beans modules form the fundamental part of the framework and include IoC (Inversion of Control) and Dependency Injection features. The idea of IoC states that instead of an application calling the methods, the framework will make the call. This idea inverses the control of the program flow. Hence, the name Inversion of Control, or IoC in short. However, IoC is a broader term and Dependency Injection (DI) is a form of its implementation. DI removes tight coupling between software components by injecting object properties through constructor and getter/setter injection. There is another type of injection, called interface injection, which is not supported by Spring. There is a sophisticated factory pattern implementation called the BeanFactory that helps to decouple configuration and specification dependencies from program logic.
- The spring-context module is built upon the spring-core and spring-beans modules and provides the way to access objects in JNDI style. The features inherited from the spring-bean module helps it to add support for internationalization, resource loading, and event propagation. Other JEE features supported by this module are EJB, JMX, and remoting. The ApplicationContext is the main interface derived from this module. The spring-context-support module provides the way to integrate many third-part libraries, such as JasperReport for reporting, Quartz for scheduling, JavaMail for mailing, and EhCache for caching.
- The spring-expression module provides a powerful Expression Language (as specified by unified EL, according to JSP 2.1 Specification) for querying and manipulating object graphs at runtime.
- The spring-aop module provides implementation for aspect-oriented programming. The main point of aspect-oriented programming (AOP) is to separate cross-cutting concerns from business logic because, although they participate in the overall operation of the program, they yet are separate conceptually. A very common example is the process of logging that may cross cut the concern of the business logic because it has no apparent need in the process but helps to log events that may help to backtrack to a certain checkpoint or debug the program in the long run.
- The spring-messaging module serves as the basis of message-based application in Spring. It provides annotation to map messages to methods similar to the annotation-based Spring MVC programming model.
- The Data access/Integration layer contains support for JDBC, ORM, OXM, JMS, and Transactions.
- The spring-jdbc module provides JDBC abstraction.
- The spring-tx module supports programmatic and declarative transaction for all POJOs.
- The spring-orm module provides support for popular ORM APIs such as JPA, JDO, Hibernate, and so on.
- The spring-oxm model is for object/XML mapping implementation such as JAXB, XStream, Castor, and XMLBeans.
- The spring-jms module is an abstraction of Java Mesaging Service (JMS). It provides support for producing and consuming messages. With Spring 4.1, it is integrated with the spring-messaging module.
- The Web layer consists of four modules: spring-web, spring-webmvc, spring-websocket, and spring-webmvc-portlet.
- The spring-web module is the foundation of Web feature integration such as multipart file upload functionality and initializing IoC container with Servlet listener. It also contains HTTP client and related parts to support remoting.
- The spring-webmvc module provides support for MVC and REST services for Web applications.
- The spring-websocket module provides the support for Web socket protocol defined by RFC 6455. This provides capability for Web application to support full-duplex communication between client and server.
- The spring-webmvc-portlet module provides support from MVC implementation for Portlet environment similar to servlet-based implementation in the spring-webmvc module.
- The spring-test module provides support for unit testing, integration testing in association with JUnit or TestNG.
It is unfair to talk about the Spring framework without any mention about Spring Boot because there is little reason to begin a Spring project without Spring Boot. It does not matter if you just want to play with Spring or want to build a production-ready application; the advice is always to use Spring Boot. Now, the point is what actually is Spring Boot? It is definitely not a framework. The answer to this is not very easy to describe in a paragraph; still, we’ll try.
Think of Spring Boot as a suite that provides a pre-configured set of libraries with all their dependencies met to reduce boilerplate configuration. It is the quickest way to create a production-ready application up and running out of the box. What Spring Boot does is that it follows convention over configuration and opines to choose a set of libraries accordingly. This is done with a little intervention of the programmer. If you have experience of resolving dependencies manually while using any framework, you would know how irritating and torturous it can be to meet dependencies. With Spring Boot, what we can do is, for example, just mention the type of application one wants to create, such as a Web application, and the technologies required from the stack of opinions; Spring Boot takes care of the rest. It imports the required libraries, their dependencies, and all the dependencies that the project type conventionally requires, such as an application server, and the like.
Packaging and deploying the production-ready application is also hassle-free. One can almost forget that the application is actually running in an embedded application server once the project jar is created. Just run the jar file and Voilà! The application runs like a standalone application.
This is, in a nutshell, what Spring Boot offers. Spring Boot is flexible enough to embrace configuration tweaks when necessary.
This is the reason why the Spring framework is associated with Spring Boot. This, however, does not mean that the Spring framework cannot be used in isolation. Spring Boot just makes it a whole lot easier to use. That’s it.
Refer to Spring Boot Reference for more detail.
As of writing this article, Spring version 4.x is the latest stable and the release candidate Spring 5 is just waiting around the corner. As of today, Spring has actually become an assemblage of many frameworks under one banner. The spectrum of projects under the banner of Spring can be found here. The core Spring framework can be found in this link. Kudos to Spring’s idea of convention over configuration. This has saved a lot of trouble for developers.