Spring Framework
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Jakarta EE platform. The framework does not impose any specific programming model. The framework has become popular in the Java community as an addition to the Enterprise JavaBeans model. The Spring Framework is free and open source software.
Version history
The first version was written by Rod Johnson, who released the framework with the publication of his book Expert One-on-One J2EE Design and Development in October 2002. The framework was first released under the Apache 2.0 license in June 2003. The first production release, 1.0, was released in March 2004. The Spring 1.2.6 framework won a Jolt productivity award and a JAX Innovation Award in 2006. Spring 2.0 was released in October 2006, Spring 2.5 in November 2007, Spring 3.0 in December 2009, Spring 3.1 in December 2011, and Spring 3.2.5 in November 2013. Spring Framework 4.0 was released in December 2013. Notable improvements in Spring 4.0 included support for Java SE 8, Groovy 2, some aspects of Java EE 7, and WebSocket.Spring Framework 4.2.0 was released on 31 July 2015 and was immediately upgraded to version 4.2.1, which was released on 01 Sept 2015. It is "compatible with Java 6, 7 and 8, with a focus on core refinements and modern web capabilities".
Spring Framework 4.3 was released on 10 June 2016 and was supported until 2020. It was announced to "be the final generation within the general Spring 4 system requirements, ".
Spring 5 was built upon Reactive Streams compatible Reactor Core.
Spring Framework 6.0 was released on 16 November 2022 and came with a Java 17+ baseline and a move to Jakarta EE 9+, with a focus on the recently released Jakarta EE 10 APIs such as Servlet 6.0 and JPA 3.1.
Modules
The Spring Framework includes several modules that provide a range of services:- Spring Core Container: this is the base module of Spring and provides spring containers. In this context,
spring-coreis the artifact found in the core module belonging to theorg.springframeworkgroup. Thespring-coreartifact consists of the IoC container, as well as the utility classes used throughout the application. - Aspect-oriented programming: enables implementing cross-cutting concerns. The
spring-aopis an artifact for the AOP framework. - Authentication and authorization: configurable security processes that support a range of standards, protocols, tools and practices via the Spring Security sub-project.
- Convention over configuration: a rapid application development solution for Spring-based enterprise applications is offered in the Spring Roo module.
- Data access: working with relational database management systems on the Java platform using Java Database Connectivity and object-relational mapping tools and with NoSQL databases. The
spring-jdbcis an artifact found in the JDBC module which supports JDBC access by including datasource setup classes. - Inversion of control container: configuration of application components and lifecycle management of Java objects, done mainly via dependency injection.
- Messaging: declarative registration of message listener objects for transparent message-consumption from message queues via Java Message Service, improvement of message sending over standard JMS APIs.
- Model–view–controller: an HTTP- and servlet-based framework providing hooks for extension and customization for web applications and RESTful Web services.
- Remote access framework: declarative remote procedure call -style marshalling of Java objects over networks supporting Java remote method invocation, CORBA and HTTP-based protocols including Web services such as SOAP.
- Transaction management: unifies several transaction management APIs and coordinates transactions for Java objects.
- Remote management: declarative exposure and management of Java objects for local or remote configuration via Java Management Extensions.
- Testing: support classes for writing unit tests and integration tests.
- WebFlux support: support for using reactive runtimes or web servers such as UnderTow and Netty.
- Web Socket support: Support for communicating using the WebSocket protocol. The artifact for this module is
spring-websocket. - XML support: support for object-toXML mapping. Libraries such as Jakarta XML Binding and XStream are supported. The artifact for this module is
spring-oxm.
Inversion of control container
The inversion of control container is the core container in the Spring Framework. It provides a consistent means of configuring and managing Java objects using reflection. The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together.In multiple cases, one need not use the container when using other parts of the Spring Framework, although using it will likely make an application easier to configure and customize. The Spring container provides a consistent mechanism to configure applications and integrates with almost all Java environments, from small-scale applications to large enterprise applications.
The programmer does not directly create an object, but describes how it should be created, by defining it in the Spring configuration file. Similarly, services and components are not called directly; instead a Spring configuration file defines which services and components must be called. This IoC is intended to increase the ease of maintenance and testing.
Creating and managing beans
Objects created by the container are called managed objects or beans. The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. These data sources contain the bean definitions that provide the information required to create the beans.The is a Spring-specific annotation that marks a class as the configuration class. The configuration class provides the beans to the Spring. Each of the methods in the Spring configuration class is configured with the annotation. The interface will then return the objects configured with the annotation as beans. The advantage of Java-based configuration over XML-based configuration is better type safety and refactorability.
Types of Inversion of Control
There are several types of Inversion of Control. Dependency injection and dependency lookup are examples of Inversion of Control. Objects can be obtained by means of either dependency lookup or dependency injection.Dependency Injection
Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods. There are several ways to implement dependency injection: constructor-based dependency injection, setter-based dependency injection and field-based dependency injection.Dependency Lookup
Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type.Autowiring
The Spring framework has a feature known as autowiring, which uses the Spring container to automatically satisfy the dependencies specified in the JavaBean properties to objects of the appropriate type in the current factory. This can only occur if there is only one object with the appropriate type.There are several annotations that can be used for autowiring POJOs, including the Spring-specific annotation , and the standard Java annotations and.
The annotation can be used on a class that defines a bean to inform Spring to prioritize the bean creation when autowiring it by name.
The annotation can be used on a class that defines a bean to inform Spring to prioritize the bean creation when autowiring it by type.
The annotation is an annotation that conforms to JSR 250, or Common Annotations for the Java Platform, and is used for autowiring references to POJOs by name.
The annotation is an annotation that conforms to JSR 300, or Standard Annotations for injection, and is used for autowiring references to POJOs by type.
Aspect-oriented programming framework
The Spring Framework has its own Aspect-oriented programming framework that modularizes cross-cutting concerns in aspects. The motivation for creating a separate AOP framework is to provide basic AOP features without too much complexity in either design, implementation, or configuration. The Spring AOP framework takes full advantage of the Spring container.The Spring AOP framework is proxy pattern-based. It is configured at run time. This removes the need for a compilation step or load-time weaving. On the other hand, interception only allows for public method-execution on existing objects at a join point.
Compared to the AspectJ framework, Spring AOP is less powerful, but also less complicated. Spring 1.2 includes support to configure AspectJ aspects in the container. Spring 2.0 added more integration with AspectJ; for example, the pointcut language is reused and can be mixed with Spring AOP-based aspects. Further, Spring 2.0 added a Spring Aspects library that uses AspectJ to offer common Spring features such as declarative transaction management and dependency injection via AspectJ compile-time or load-time weaving. SpringSource uses AspectJ AOP in other Spring projects such as Spring Roo and Spring Insight, with Spring Security offering an AspectJ-based aspect library.
Spring AOP was designed to work with cross-cutting concerns inside the Spring Framework. Any object which is created and configured by the container can be enriched using Spring AOP.
Since version 2.0 of the framework, Spring provides two approaches to the AOP configuration:
- schema-based approach and
-
@AspectJ-based annotation style.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
The Spring team decided not to introduce new AOP-related terminology. Therefore, in the Spring reference documentation and API, terms such as aspect, join point, advice, pointcut, introduction, target object, AOP proxy, and weaving all have the same meanings as in most other AOP frameworks.