Spring Framework is an open-source framework for building web applications with Java as a programming language. It is powerful and lightweight yet easy to use, and it provides support for developing Java applications easily. Spring is a lightweight framework which can be thought of as a framework of frameworks because it also offers support for various frameworks such as hibernate, struts, tapestry, and JSF.
In this Java Spring tutorial, you will learn:
- What is Spring framework?
- Features of Spring Framework
- Brief History of Spring
- Spring Framework Architecture
- Spring – MVC Framework
- Aspect-Oriented Programming
- Installation of Java Spring
- Advantages of Spring Framework
- Disadvantages of Spring Framework
Now in this Spring tutorial, we will cover some most prominent features of Spring Framework:
- Predefined templates
- Easy to test
- Loose coupling
- Fast development
- Powerful abstraction
- Offers an array of resources
- Declarative support
- Offers comprehensive tools
Now in this Spring Framework tutorial, we will cover some important landmarks form the history of Spring:
- Spring framework was written by Rod Johnson and was first released in June 2002.
- Spring last version release in March 2004
- Spring 1.2.6 version release in 2006
- Spring 2.0 version release in Oct 2006
- Spring 2.5 version release in Nov 2007
- Spring 3 version release in Dec 2009
- Spring 3.1 version release in Dec 2011
- Spring framework 4 version release in Dec 2013 with Java 8 support
- Spring framework 4.2.0 version released on July 2015
- Spring framework 4.2.1 version released in Sept 2015
- Spring Framework 4.3 version released on 10 June 2016
- Spring framework 5.0 version released on June 2017
The Spring Framework Architecture provides 20 modules which can be used based on an application requirement.
The Core and Bean provide the fundamental part of the framework, including IoC and DI.
The Core Container is further divided by subcomponents like Core, Beans, Context, and Expression Language modules.
Let see each of these models in detail:
The Core module provides all the primary component of the spring framework. It includes IoC (Inversion of Control) and Dependency Injection features.
This module offers BeanFactory, which is a sophisticated implementation of the factory pattern.
The Context module builds on the solid base provided by the Core and Beans modules, and it is a medium which helps you to access any objects defined and configured.
Spring Expression Languages (SpEL):
This module offers expression language for modifying and querying object graph during the runtime.
The Data Access and Integration layer consist of the JDBC, ORM, JDBC, OXM, JMS, and Transaction modules.
- ORM: ORM module offers consistency/ portability to code irrespective of data access technologies. It will be based on object-oriented mapping concept.
- The JDBC module consists of a JDBC-abstraction layer. It helps you elements the need to perform JDBC related coding.
- OXM: Object XML Mappers (OCM) helps you to convert the objects into XML format and vice versa.
- The Java Messaging Service module offers features like producing and consuming messages.
- Transaction: This module offers declarative and programmatic management method for implementing unique interfaces and for all types of POJOs (Plain Old Java Object)
Web: This module uses servlet listeners and a web-oriented application context. It also offers a feature of web-oriented integration and functionality for multi-part file upload.
Web-servlet: This module stores MVC based implementation for web applications.
Web-Socket: Module offers WebSocket based and two-way communication between the client and the server in web apps.
Web-Portlet: This module is also called Spring-MVC-Portlet module. It offers for Spring-based Portlets and copies all the functionality of a Web-Servlet module.
AOP: AOP language is a useful tool which allows developers to add enterprise functionality to the application.
Instrumentation: This module offers class instrumentation and loader implementations. It is used for specific application servers.
Test: This module provides support for testing of Spring components with TestNG or JUnit tools. It offers consistent loading of Spring ApplicationContexts and caching of those contexts.
Now in this Spring Framework tutorial, we will learn about Spring MVC framework:
The Spring web MVC framework offers model-view-controller architecture offers components which helps you to be flexible and loosely coupled web applications.
The MVC pattern allows you to separate different aspects of the application while offering loose coupling between these elements. Spring MVC also helps you to build flexible and loosely coupled web applications.
The MVC design also allows you to separate business logic, presentation logic, and navigation logic. It also offers an elegant solution to use MVC in Spring Framework with the help of DispatcherServlet.
How MVC works in Spring?
Now in the Spring tutorial, we are going to learn how MVC works in Spring:
- DispatcherServlet receives a request.
- After that, the DispatcherServlet communicates with HandlerMapping. It also revokes controller associated with that specific request.
- The Controller processes this request by calling the service methods, and a ModelAndView object returned by the DispatcherServlet.
- The view name is sent to a ViewResolver to search the actual View to invoke.
- After that, DispatcherServlet is passed to View to render the result.
- By using the model data, the View renders and send back result back to the user.
Aspect-oriented programming allows the global properties of a program to decide how it is compiled into an executable program.
The key unit of modularity is an aspect instead of a class. Aspect-Oriented programming breaks down the logic of the program into various parts known as concerns.
A cross-cutting concern may affect the entire application and is centralized in one location in code like transaction security, management, logging, authentication, etc.
It can also be considered as a dynamic decorator design pattern. This pattern also offers an additional behavior to be added to an existing class by wrapping the original class and duplicating its interface and removing the original.
Core AOP Concepts
There are seven core concepts in Aspect-Oriented Programming.
Let’s discuss them in detail:
- Aspect: An aspect is a class which allows implementing the JEE application. It concerns which cut across multiple classes, like transaction management, security, etc.
Aspects can be a typical class configured using Spring XML configuration or a regular class annotated using @Aspect annotation.
- Joinpoint: It is a candidate point in the program execution where an aspect can be plugged in. It could be a method being called as an exception being thrown, or even a field which is modified.
- Advice: Advice are the actual actions taken for a specific joinpoint. This method will execute when a specific joinpoint meets a matching point in the application.
- Pointcut: It is an expression which can be matched with join points to check whether that advice should be executed or not.
- Target Object: Based on target object devices are applied. In AOP, a subclass is created at runtime where the target method should be overridden, and advice are included depending on their configuration.
- Proxy: This object should be created after applying the advice to the target object.
- Weaving: This is a method of linking an aspect with other application objects or types to build an advised object.
Now in this Java Spring tutorial, we will learn the step by step information to install Java Spring in your computer:
Step 1) Go to www.eclipse.org.
Step 2) Download Eclipse Neon installer and install it on your Windows system.
Step 3) Click on “Download 64-bit” button.
Step 4) Go to Eclipse IDE for Java Developers and click on “64-bit” link.
Step 5) Click on “Windows 64-bit” link.
Step 6) Click on the “Download” button to download the zip file.
Step 7) Extract the download zip to a specific folder.
Step 8) Click on the “Browse” button to select the workspace directory.
Step 9) Click on “Select Folder” to select a folder.
The process will be displayed as shown in the following screen.
Step 10) Opening Eclipse Marketplace Client.
- Go to the Help menu.
- Click on “Eclipse Marketpalce” option.
Step 11) Searching and installing Spring plugin.
- Write Spring in “Find” textbox.
- Click on the “Search” icon.
- Click on the “Install” button to install a plugin.
Step 12) Confirm selected features and click on “Confirm” button.
Step 13) Accepting terms and condition and installing the software.
- Click on “I accept the terms of the license agreements” radio button.
- Click on “Finish” button” to finish the installation.
A welcome screen will be displayed with software installation progress.
Step 14) Click on “Restart Now” button to restart Eclipse IDE and apply the changes.
The following screen will be displayed:
Here, are important pros/ benefits of using spring.
- Spring allows developers to develop enterprise-class applications with the help of POJOs.
- Offers templates for Hibernate, JDBC, Hibernate, JPA, etc. to reduce avoid writing lengthy code.
- Provides abstraction to Java Enterprise Edition (JEE).
- You can organize a spring in a modular fashion. So that if a number of packages and classes are substantial, you only need to about you need and ignore the rest.
- It offers declarative support for transactions, formatting, validation, caching, etc.
- The application developed using spring is simple as the environment-dependent code is moved into this framework.
Here, are drawbacks/cons of Spring Framework.
- Java spring offers too many restrictions.
- Code is public is it is visible to all.
- It also not offers custom-built features.
- Spring framework is an easy to use open-source Java platform
- Predefined templates and loose couple are important features of Java spring
- Spring framework was written by Rod Johnson and was first released in June 2002
- The Spring Framework provides 20 modules which can be used based on an application requirement.
- Java spring dispatcherServlet communicates with HandlerMapping. It also revokes controller associated with that specific request.
- Aspect-oriented programming allows the global properties of a program to decide how it is compiled into an executable program.
- The biggest advantage of Java Spring is that it allows developers to develop enterprise-class applications with the help of POJOs
- The biggest drawback of Java spring is that it offers too many restrictions.