What is Spring?
Spring Framework is a technology that was developed in response to the complexity of developing Java applications using the Java enterprise framework J2EE. The spring framework was built to remove complexities and help make things like web development and data access easier for developers to implement.
One more aim of building this framework was to reduce the boilerplate code. Boilerplate code is that code that is repeated throughout the application's source code and it does not contain any business logic. So, by removing this obstacle the developers can focus on the main business logic while being productive at the same time.
Many projects were made in the Spring family to achieve these aims and it was pretty successful then came the Spring Boo which changed the entire scenario. Spring boot was built on top of the spring family.
Features of Spring Boot
AutoConfiguration: This is a very useful feature in Spring Boot. It automatically configures and set up an application based on its surroundings and some hints provided by the developer. These hints that are mentioned here are annotations.
These are One or two-word commands like@EnableAutoConfiguration
You can find a detailed article about these annotations here:
Standalone: Spring Boot supports standalone functionality. What that means is, you don't need to deploy your application into a web server or any other environment to run it. You can just run the application with one command.
The command to run a Spring Boot application is:java -jar demoApplication.jar
Opionnated View: Spring Boot has an opinionated view on doing certain things. Now you may think that you may not be interested in that but that is not a bad thing in any way. While building a Java application you have tons of choices. To reduce this burden Spring Framework is intelligent enough to set up your project so that you don't have to. Spring Boot has a soft opinion on these things, now what that means is that you can override these pre-applied settings later in the project.
Key Areas in Spring
There are six key areas in Spring. We'll cover each area in detail.
Spring Core
It is the foundation module on which other modules are built. It provides several functionalities:
Internationalization Support
Validation support
Data binding support
Type conversion support
Dependency Injection
Dependency injection in these functionalities is the most important functionality in Spring Boot. Dependency injection creates and maintains objects and their dependencies. Spring core manages these dependencies and connects them all together. One can say Spring Core is the glue of the project that binds everything together.
Spring Web
Spring Web Services (Spring-WS) is a product of the Spring community focused on creating document-driven Web services. Spring web in Spring framework does the job of handling web requests and it does the job by using mainly two ways Spring MVC and Spring WebFlux.
Spring Web MVC:
Spring Web MVC (Model-view-controller) is a project in Spring Framework that is used to build web applications. This method is built upon the functionality of a Java ServletAPI. A servlet in Java is an object that receives a request and generates a response based on that request. ServletAPI is a way in Java that is also used to build Java web applications but in using this way there are a few challenges:
The API that is built up using ServletAPI is somewhat Low-Level.
It is not easy to use.
Productivity is decreased to a great extent due to the lack of organization.
This is where Spring MVC comes into action. All these challenges above are easily solved when using MVC. The advantages of using MVC are:
The API that is built using Spring MVC is a higher-level API.
It is easy to use
Productivity is increased as it is fully organized.
Spring Web WebFlux
To get an understanding of this project in the Spring family we first need to understand the concept of Reactive Programming.
Reactive Programming: It is a declarative programming paradigm concerned with data streams and the propagation of change. It focuses on streams of data and how they change. In short, you react to change rather than wait for change.
Spring WebFlux uses this concept to gain a certain edge over other methods. Spring WebFlux has a different way of handling web requests. The web Requests are handled Asynchronously, what that means is that in WebFlux when a request is made to the resources then those resources are never in a "wait" or "block" state. The code simply asks to be notified when the operations are complete. This helps in better resource allocation and utilization.
Spring AOP
Aspect Oriented Programming is a way of writing code that increases organization by allowing the separation of cross-cutting concerns. To give you a better understanding let's take an example to understand this concept- If you observe any banking application or website then you can see that security in the application is not contained to any one area of the application instead, it spans various areas of the application.
Spring AOP is used to implement various features in Spring and is a valuable tool for developers to handle cross-cutting concerns.
Example implementation in Spring Boot:
In the below code the Annotation checks if the person accessing the application is admin or not. Yes, Spring Boot is smart enough to do that on its own by just using one line of code
@PreAuthorize("hasRole('admin)")
public void sensetiveOperation(){
//do sensetive operation
}
Spring Data Access
This module in the Spring framework helps in making applications that interact with data. This module makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks and cloud access databases. This is an umbrella project which contains sub-projects that are specific to a given database.
Here is a code example in which we are using a JdbcTemplate
in Spring to reduce boilerplate code. Here Foo is a database table in MySQL.
int cnt=new JdbcTemplate(ds)
.queryForInt("SELECT COUNT(*) FROM FOO");
Database Transactions with the Spring Framework's Data Access module are really easy. A database transaction is a series of database operations that must happen together or not at all.
Here is another code example:
@Transactioal
public void operaation(){
//Operations in the database
}
One more feature of this module is the Exception Translation. If you tried to retrieve some data from a database and it throws an error, then there is always an error code associated with it. This error code is different for every database even though it is the same error. What this module does is that it associates that same kind of error with a single exception. So Vendor-specific exceptions are mapped into a well-known set of exceptions. Testing is also easy with this module. It is easy to switch between configurations and if any data is pushed in the database for testing then that data is removed after the testing is finished automatically by this module.
Spring Integration
Integration is all about making different systems and applications work together. It solves the problem of how one application talks to another application. Now there are two questions to this:
How do you expose operations to another system?
How do you run those operations on another system?
There are three approaches to solving this issue:
RMI (Remote Method Invocation).
Messaging Systems.
Web Services
In the third method, We simply expose our operations to the web and then those are accessible from there.
We can use a RestTemplate
with which we can programmatically invoke a rest service:-
restTemplate.getForObject("http://Bank/accounts", Accounts.class);
Spring Testing
This module in the Spring Framework makes it easy to test the source code very easily. There are two types of testing mainly:
Unit testing- Unit testing is where we test any smallest unit of code possible. Explicit support for unit testing is minimal. Benefits come from dependency injection. We can control how a dependency behaves and only test the code and not the dependency. This is known as mock dependency. Spring framework comes with several pre-written mocks to make testing easier and faster.
Integration testing- The phase in software testing in which individual software modules are combined and tested as a group. Spring framework provides support for piecing together parts of an application for testing, Like testing with data and Web application testing.
Spring framework provides support for cleaning up after tests. Tests modify things and those modifications should be reversed after testing is complete. Spring Testing module does just that.
Conclusion
Understanding the Spring Framework is essential for any Java developer seeking to build modern and efficient applications. In this comprehensive guide, we've explored the core concepts, key features, and various modules that constitute the Spring ecosystem. By embracing Spring's simplicity, developers can enhance their productivity and focus on delivering high-quality applications. So, embark on your Spring journey with confidence, and unlock the full potential of Java application development. Happy Developing!!