Introduction to Spring Framework

Spring is an Open Source, Lightweight, Container and Framework for building Java enterprise applications

Spring is Open Source

The Spring Framework is licensed according to the terms of the Apache License, Version 2.0.

Spring binary and source code is freely available:

Spring is Lightweight

  • Spring applications do not require a Java EE application server, but they can be deployed on any of them (Glassfish, Weblogic, Websphere, JBoss, etc)
  • Spring is not invasive, it does not require us to extend framework classes or implement framework interfaces for most usage. We can simply write our code as POJOs (Plain Java Old Objects)
  • Low overhead, Spring jars are relatively small

Let's brainstorm or debates about the last two points:

Spring is not invasive

  • Spring framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API. Most of the java frameworks forcing us to extend one of their classes or implement one of their interfaces. Example of such an invasive programming model was EJB 2-era stateless session beans, earlier versions of Struts, WebWork, Tapestry, etc. In (earlier version of) Struts, we are used to extend Action class, that’s why Struts is said to be invasive.
  • An invasive IoC container requires us to use the framework's annotations or extend framework classes/interfaces, while a non-invasive container allow us to use standard Java, i.e., javax.inject.Inject. For this case, We can call Spring non-invasive because in Spring container (which is an IoC Container) we can just use standard Java (non-invasive) annotations @Inject, not only Spring specific annotations @Autowired.

Spring jars are relatively small

Spring is a Container

  • Spring is an IoC ( Inversion of Control) Container for our application objects, so these objects able to find and connecting to each other.
  • Spring instantiates and dependency injects our objects and serves as a life cycle manager

Please check Core Concepts Of The Spring Framework.

Spring is a Framework

Enterprise applications must deal with a wide variety of technologies / resources: JDBC, JMS, AMQP, Transactions, ORM / JPA, NoSQL, Security, Web, Tasks, Scheduling, Mail, Files, XML/JSON, Marshalling, Remoting, REST services, SOAP services, Mobile, Social, and many more.

Spring provides framework classes to simplify working with those technologies.

Please check Spring Framework Overview.

Why (we have) Spring?

Long story make short, let's start with brief history of Java:

The early years:

  • 1995 – Java introduced, Applets are popular
  • 1997 – Servlets introduced; efficient, dynamic web pages become possible.
  • 1999 – JSP introduced; efficient, dynamic web pages become easy.

Questions arise regarding “Enterprise” applications; how should a Servlet / JSP application handle:

  • Persistence
  • Transactions
  • Security
  • Business Logic
  • Messaging
  • Etc, etc, etc...

Introducing J2EE and EJB

  • 1999 – Java's answer: J2EE

To answers those questions J2EE is introduced, featuring Enterprise Java Beans (EJB).

However EJBs prove to be problematic:

  • Difficult to code: must extend / implement specific classes /interfaces and complicated programming model required, including many configurations. Don't believe me? See below code:
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;

public class ConverterBean implements SessionBean {

    BigDecimal yenRate = new BigDecimal("121.6000");
    BigDecimal euroRate = new BigDecimal("0.0077");

    public BigDecimal dollarToYen(BigDecimal dollars) {
        BigDecimal result = dollars.multiply(yenRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }

    public BigDecimal yenToEuro(BigDecimal yen) {
        BigDecimal result = yen.multiply(euroRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }

    public ConverterBean() { }

    public void ejbCreate() { }
    public void ejbRemove() { }
    public void ejbActivate() {}
    public void ejbPassivate() { }
    public void setSessionContext(SessionContext sc) { }
}
                    

see implements SessionBean and methods ejbCreate(), ejbRemove(), ejbActivate(), ejbPassivate(), etc. Read again the part about invasive vs non-invasive framework.

EJB also:

  • Difficult to do unit test
  • Expensive to run: must have J2EE compliant application server with resource intensive.

The Birth of Spring

In 14 June 2004, Expert One-on-One J2EE Development without EJB is published (author:Rod Johnson, contributor: Juergen Hoeller)

  • 2004 - Spring Framework 1.0 released

Spring championing dependency injection and encourages POJOs instead of J2EE way to extend / implement specific classes /interfaces. Spring also use XML files to describe application configuration.

Spring becomes popular quickly as an EJB alternative

Spring Framework History

  • Spring 2.0 (2006): XML simplification, async JMS, JPA, AspectJ support
  • Spring 2.5 (2007, last release 2.5.6): Annotation DI, @MVC controllers, XML namespaces. Requires Java 1.4+ and supports JUnit 4
  • Spring 3.x (3.2.17 released July 2016): Environment & Profiles, @Cacheable, @EnableXXX …, REST support, JavaConfig, SpEL, more annotations. Requires Java 1.5+ and JUnit 4.7+
  • Spring 4.x (released Dec 2013): Support for Java 8, @Conditional, Web-sockets
  • Spring 5.x (2017): Reactive programming focus

Spring is not simply an alternative to J2EE / EJB. Modern application development challenges are different today compare to 10 years ago or than in 2000s (2004 when Spring 1.0 introduced). Spring still continues to innovate and relevant to current technology needs:

  • Web: AJAX, WebSockets, REST, Mobile, Social
  • Data: NoSQL, Big Data, Stream processing
  • Cloud: Distributed systems, Cloud, Microservices
  • Productivity: Spring Boot, Spring Cloud Data Flow
  • etc, etc, etc