Spring Framework: Difference between revisions

Content deleted Content added
Reverted 1 edit by 183.82.111.205 (talk): Test edit
 
(14 intermediate revisions by 9 users not shown)
Line 1:
{{About|the Spring Framework|the Spring Boot|Spring Boot||}}
{{Short description|Application framework for Java platform}}
{{Infobox software
Line 24 ⟶ 25:
! Version
! Date
! Notes
|-
| 0.9 || 2003
| 2003
|-
| 1.0 || March 24, 2004
| March 24, 2004
| First production release.
|-
| 2.0 || 2006
| 2006
|-
| 3.0 || 2009
| 2009
|
|-
| 4.0 || 2013
| 2013
|
|-
| 5.0 || 2017
| 2017
|
|-
| 6.0 || November 22, 2022
| 6.0
|-
| November 22, 2022
| 6.1 || November 16, 2023
|
|-
| 6.2 || November 14, 2024
|-
|}
 
The first version was written by [[Rod Johnson (programmer)|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 License|Apache 2.0 license]] in June 2003. The first production release, 1.0, was released in March 2004.<ref>{{cite web |url=https://spring.io/blog/2004/03/24/spring-framework-1-0-final-released | date=24 March 2014 | title=Spring Framework 1.0 Final Released | website=Official Spring Framework blog |access-date=1 March 2021}}</ref> The Spring 1.2.6 framework won a [[Jolt Awards|Jolt productivity award]] and a [[JAX Innovation Award]] in 2006.<ref>[http://www.ddj.com/architect/187900423?pgno=10 Jolt winners 2006]</ref><ref>{{Cite web |url=http://jax-award.de/jax_award06/gewinner_de.php |title=JAX Innovation Award Gewinner 2006 |access-date=2009-08-12 |archive-url=https://web.archive.org/web/20090817202514/http://jax-award.de/jax_award06/gewinner_de.php |archive-date=2009-08-17 |url-status=dead }}</ref> 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.<ref>{{cite web |url=https://spring.io/blog/2013/11/07/spring-framework-3-2-5-released |date=7 Nov 2013 | title=Spring Framework 3.2.5 Released | website=Official Spring website | access-date=16 October 2016}}</ref> Spring Framework 4.0 was released in December 2013.<ref>{{cite web|url=https://spring.io/blog/2013/12/12/announcing-spring-framework-4-0-ga-release/|title=Announcing Spring Framework 4.0 GA Release|publisher=Spring blog|date=12 December 2013}}</ref> Notable improvements in Spring 4.0 included support for Java SE (Standard Edition) 8, [[Groovy (programming language)|Groovy]] 2,{{sfn | Walls | 2016 | loc=§5 | pp=92-106}}{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc=§4 Spring Configuration in Detail and Spring Boot | pp=125-126 }} some aspects of Java EE 7, and [[WebSocket]].{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc = §1 Introducing Spring | pp=1-18}}
{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc=§4 Spring Configuration in Detail and Spring Boot | pp=125-126 }} some aspects of Java EE 7, and [[WebSocket]].
{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc = §1 Introducing Spring | pp=1-18}}
 
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.<ref>{{cite web|url=http://spring.io/blog/2015/07/31/spring-framework-4-2-goes-ga|title=Spring Framework 4.2 goes GA|publisher=Spring Blog|date=31 July 2015}}</ref> It is ''"compatible with Java 6, 7 and 8, with a focus on core refinements and modern web capabilities"''.<ref name="spring-4.2">{{cite web|url=http://spring.io/blog/2015/07/31/spring-framework-4-2-goes-ga|title=Spring Framework 4.2 goes GA|publisher=Spring Blog}}</ref>
Line 74 ⟶ 62:
* [[Authentication]] and [[authorization]]: configurable security processes that support a range of standards, protocols, tools and practices via the [[Spring Security]] sub-project (formerly Acegi Security System for Spring).{{sfn|Johnson|Höller|Arendsen|Risberg|2005|loc=Acegi Security System for Spring}}{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§7 Spring Security | p=331}}
* [[Convention over configuration]]: a rapid application development solution for Spring-based enterprise applications is offered in the [[Spring Roo]] module.
* [[Data access]]: working with [[RDBMS|relational database management systems]] on the Java platform using [[Java Database Connectivity]] (JDBC){{sfn|Walls|2019|pp=56-59}} and [[object-relational mapping]] tools and with [[NoSQL]]{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§10 Data Access | pp=419-426}} databases. The <code>spring-jdbc</code> is an artifact found in the JDBC module whilewhich supports JDBC access by including datasource setup classes.{{sfn | Deinum | Cosmina | 2021 | loc=§2 Spring Framework Fundamentals - The Spring Framework | pp=22-25}}
* [[Inversion of control]] container: configuration of application components and lifecycle management of Java objects, done mainly via [[dependency injection]].{{sfn|Johnson|Höller|Arendsen|Risberg|2005|loc=Chapter §2 - The Bean Factory and ApplicationContext}}
* Messaging: declarative registration of message listener objects for transparent message-consumption from [[message queue]]s via [[Java Message Service]] (JMS), improvement of message sending over standard JMS APIs.{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§15-4 Create Message-Driven POJOs in Spring | pp=677-681}}
Line 99 ⟶ 87:
Objects created by the container are called managed objects or [[JavaBeans|beans]].{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§2-16 Use Property Editors in Spring|pp=112-116}} The container can be configured by loading [[XML]] (Extensible Markup Language) files{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§2-1 Manage and Configure POJOs with the Spring IoC Container | pp=48-52}}{{sfn | Johnson | Hoeller | 2004}}{{rp|pp=151–152}} or detecting specific [[Java annotation]]s on configuration classes. These data sources contain the bean definitions that provide the information required to create the beans.
 
The {{code|@Configuration}} is a Spring-specific annotation that marks a class as the configuration class. The configuration class provides the beans to the Spring {{code|ApplicationContext}}.{{sfn|Walls|2019|loc=§1.1 Getting started with Spring - What is Spring|pp=4-6}} Each of the methods in the Spring configuration class is configured with the {{code|@Bean}} annotation. The {{code|ApplicationContext}} interface will then return the objects configured with the {{code|@Bean}} annotation as beans. The advantage of javaJava-based configuration over XML-based configuration is better type safety and refactorability.{{sfn|Walls|2019|loc=§1.1 Getting started with Spring - What is Spring | pp=4-6}}
 
==== Types of Inversion of Control ====
Line 112 ⟶ 100:
 
====Autowiring====
The Spring framework has a feature known as autowiring, which uses the springSpring container to automatically satisfy the dependencies specified in the JavaBean properties to objects of the appropriate type in the current factory. {{sfn | Johnson | Hoeller | 2004 | loc=§6 Lightweight Containers and Inversion of Control - IOC Containers | pp=135–137}} This can only occur if there is only one object with the appropriate type.{{sfn | Johnson | Hoeller | 2004 | loc=§6 Lightweight Containers and Inversion of Control - IOC Containers | pp=135–137}}
 
There are several annotations that can be used for autowiring POJOs, including the Spring-specific annotation {{code|@Autowire}} (as well as several other Spring-specific annotations that help resolve autowire ambiguity such as the {{code|@Qualifier}} or {{code|@Primary}} annotations),{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§3-3 Use POJO References and Auto-Wiring to Interact with other POJOs |pp=145-151}}{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc=§3 Introducing IoC and DI in Spring - Autowiring Your Beans | pp=112-120 }} and the standard Java annotations {{code|@Resource}} and {{code|@Inject}}.{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§3-4 Auto-wire POJOs the @Resource and @Inject annotation | pp=151-154}}
 
The {{code|@Qualifier}} annotation can be used on a class that defines a bean to inform Spring to prioritize the bean creation when autowiring it by '''name'''.{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc=§3 Introducing IoC and DI in Spring - Autowiring Your Beans | pp=112-120 }}
 
The {{code|@Primary}} annotation can be used on a class that defines a bean to inform Spring to prioritize the bean creation when autowiring it by '''type'''.{{sfn | Cosmina | Harrop | Schaefer | Ho | 2017 | loc=§3 Introducing IoC and DI in Spring - Autowiring Your Beans | pp=112-120 }}
 
The {{code|@PrimaryResource}} annotation canis bean usedannotation onthat aconforms classto that[[JSR defines250]], aor beanCommon toAnnotations informfor Springthe toJava prioritizePlatform, theand beanis creationused whenfor autowiring itreferences to POJOs by type'''name'''.{{sfn | CosminaDeinum | HarropLong | SchaeferMak | HoRubio | 20172014 | loc=§3-4 IntroducingAuto-wire IoCPOJOs andthe DI@Resource inand Spring@Inject - Autowiring Your Beansannotation | pp=112151-120 154}}
 
The {{code|@ResourceInject}} annotation is an annotation that conforms to [[JSR 250]], or Common Annotations for the Java Platform. The {{code|@Resource}} annotation is used for autowiring references to POJOs by name.{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§3-4 Auto-wire POJOs the @Resource and @Inject annotation | pp=151-154}} The {{code|@Inject}} annotation is annotation that conforms to JSR 300, or Standard Annotations for injection., The {{code|@Inject}} annotationand is used for autowiring references to POJOs by '''type'''.{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§3-4 Auto-wire POJOs the @Resource and @Inject annotation | pp=151-154}}
 
===Aspect-oriented programming framework===
Line 132 ⟶ 122:
Spring AOP has been designed to work with cross-cutting concerns inside the Spring Framework.{{sfn | Johnson | Hoeller | 2004}}{{rp | p=473}} Any object which is created and configured by the container can be enriched using Spring AOP.
 
The Spring Framework uses Spring AOP internally for transaction management, security, remote access, and [[Java Management Extensions|JMX]].<ref>{{Citationcite book |last=Chidester |first=Ashlan |title=Looking Forward to the Spring Framework needed|date=June2024 |publisher=Kindle Edition |url=https://www.amazon.com/dp/B0CFYBPFLQ?ref=KC_GS_GB_US |access-date=February 12, 20232025}}</ref>
 
Since version 2.0 of the framework, Spring provides two approaches to the AOP configuration:
Line 200 ⟶ 190:
* Transactions managed on a JDBC Connection{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§11-2 Choosing a Transaction Manager Implementation|pp=464-468}}
* Transactions managed on Object-relational mapping Units of Work{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§11-2 Choosing a Transaction Manager Implementation|pp=464-468}}
* Transactions managed via the JTA {{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§11-2 Choosing a Transaction Manager Implementation|pp=464-468}}<code>JtaTransactionManager</code>{{sfn | Deinum | Long | Mak | Rubio | 2014 | loc=§11-3 Managing Transactions Programmatically with the Transaction Manager API|pp=484-486}}{{sfn | Johnson | Hoeller | 2004}}{{rp | pp=255–257}} and <code>UserTransaction</code>{{sfn | Johnson | Hoeller | 2004}}{{rp | p=234}}
* Transactions managed on other resources, like [[object database]]s
 
Line 256 ⟶ 246:
* By using <code>javax.servlet.ServletContainerInitializer</code>{{sfn|Deinum|Cosmina|2021|loc=§4 Spring MVC Architecture - Bootstrapping DispatcherServlet|pp=84-90}}
* By implementing the <code> org.springframework.web.WebApplicationInitializer</code> interface.{{sfn|Deinum|Cosmina|2021|loc=§4 Spring MVC Architecture - Bootstrapping DispatcherServlet|pp=84-90}}
* By using the built-in autoconfiguration for Spring Boot, which uses the <code>[[Spring Boot#Bootstrapping DispatcherServlet|SpringBootServletInitializer]]</code> class.lm{{sfn|Deinum|Cosmina|2021|loc=§4 Spring MVC Architecture - Bootstrapping DispatcherServlet|pp=84-90}}
 
===Remote access framework===
Line 296 ⟶ 286:
* Provide opinionated 'starter' [[Project Object Model]]s (POMs) to simplify your [[Maven (software)|Maven]]/[[Gradle]] configuration{{sfn|Walls|2016|loc=§Preface|p=x}}
* Automatically configure Spring whenever possible{{sfn|Walls|2016|loc=§1.1.2|pp=4-5}}
* Provide production-ready{{sfn|Walls|2016|loc=§foreword|p=vii}} features such as [[software metrics|metrics]],{{sfn|Walls|2016|loc=§7|pp=124-139}} health checks {{sfn|Walls|2016|loc=§7|pp=124-139}} and externalized configuration{{sfn|Walls|2016|loc=§3.1-§3.2.3|pp=49-69}}
* Absolutely no code generation{{sfn|Walls|2016|loc=§1.1.3|p=7}} and no requirement{{sfn|Walls|2016|loc=§Preface|p=x}} for XML configuration.<ref>{{cite web | url=https://aristeksystems.com/blog/what-you-need-know-about-spring-boot/| title=About Spring Boot | accessdate=2020-03-18}}</ref>
* Smooth Integration and supports all Enterprise Integration Patterns.
Line 337 ⟶ 327:
* gateways - exposes an interface to the client for the requested services. A messaging middleware is responsible for provisioning this interface. This interface decouples the messaging middleware from the client by hiding the underlying JMS or Spring Integration APIs. Gateways are related to the [[Facade pattern]]. Spring's Integration class, <code>SimpleMessagingGateway</code>, provides essential support for gateways. <code>SimpleMessagingGateway</code> enables the Spring application to specify the channel that sends requests, and the channel that expects to receive responses. The primary focus of <code>SimpleMessagingGateway</code> is to deal with payloads, which spares the client from the intricate details of the transmitted and received messages. <code>SimpleMessagingGateway</code> is used along with channels to enable integration with file systems, JMS, e-mail, or any other systems that require payloads and channels.{{sfn|Deinum|Long|Mak|Rubio|2014|loc=§16-10 Using Gateways|pp=717-722}}
* splitter - Separates a large payload into smaller payloads to support different processing flows. The splitter is achieved in Spring using the splitter component. The splitter component usually forwards the messages to classes with more specialized functionality. Spring supports the <code>@Splitter</code> annotation to declare the component that requires this functionality.{{sfn|Deinum|Long|Mak|Rubio|2014|loc=§16-7 Forking Integration Control: Splitters and Aggregators|pp=710-713}}
* aggregator - Used for combining many messages into a single result. Loosely speaking, the aggregator is the reverse of the splitter. The aggregator publishes a single message for all components downstream. Spring supports the <code>@Aggregator</code> annotation to declare the component that requires this functionality. {{sfn|Deinum|Long|Mak|Rubio|2014|loc=§16-7 Forking Integration Control: Splitters and Aggregators|pp=710-713}}
 
Spring Integration supports pipe-and-filter based architectures.
 
===Spring WebSocket===
An essential rule for dealing with data streams effectively is to never block.{{sfn|Deinum|Cosmina|2021|loc=§11 The WebSocket Protocol|pp=422-425}} The WebSocket is a viable solution to this problem.{{sfn|Deinum|Cosmina|2021|loc=§11 The WebSocket Protocol|pp=422-425}} The WebSocket Protocol is a low-level [[transport protocol]] that allows [[Full duplex|full-duplex]] communication channels over a [[TCP connection]]. The WebSocket acts as an alternative to HTTP to enable two-way communication between the client and the server. The WebSocket is especially useful for applications that require frequent and fast exchanges of small data chunks, at a high speed and volume.{{sfn|Deinum|Cosmina|2021|loc=§11 The WebSocket Protocol|pp=422-425}}
 
Spring supports the WebSocket protocol by providing the WebSocket API for the reactive application. The <code>@EnableWebSocket</code> annotation gives Websocket request processing functionality when places in a Spring configuration class. A mandatory interface is the <code>WebSocketConfigurer</code> which grants access to the <code>WebSocketConfigurer</code>. Then, the Websocket URL is mapped to the relevant handlers by implementing the registerWebSocketHandlers(WebSocketHandlerRegistry) method .{{sfn|Deinum|Cosmina|2021|loc=§11 The WebSocket Protocol|pp=425-432}}
 
===Spring WebFlux===