The Spring way of developing Java Web Apps.
In our last Episode 2 https://www.garudax.id/pulse/building-blocks-web-application-development-java-osman-mohammed/) we learned about building blocks of web application development in Java. In this episode, we are going to learn about the below things.
1) What is Spring Framework?
2) What is Inversion of Control and Dependency Injection?
So let's get started. Just a heads up, this section might be a little overwhelming for some of you folks.
1) What is Spring Framework?
As stated in previous articles to fully understand Spring we need to understand what problem does Spring solves?
- The answer is, working with application servers means learning a new way to build java applications, and there's still a ton of work left for the developer, like configuring Servlets mappings and condensing complex application logic into a single Servlets service method. Servlets and app servers together form a powerful platform, and on top of that platform lies Spring.
- Spring is many things, but primarily, it's an application framework. Unlike a library from which we would pick and choose specific APIs to invoke from our application, we use an application framework to instead invoke our application's API's in some pre-defined way. This pattern for application development is called inversion of control or IoC, and it's implemented by Springs core module. We can define individual application components as classes that the Spring IoC container instantiates and manages just like an application server instantiates and manages Servlets. In fact, when we use Spring in a web application, we're piggy-backing the Spring container on the server that instance, meaning that the overall life cycle of our Spring application components is bounded by the lifecycle of the Servlets we're running Spring on. It also means that Spring can reroute requests from the Servlets layer to our application components according to whatever logic we dictate. Ultimately, that's the primary advantage of using Spring as an IOC container. We can easily define components of our application for specialized tasks and then connect them with one another automatically to perform complex operations. For example, we might have one component that receives requests from the Servlets layer and is responsible for validating the request and returning a response. In the course of processing the request, it might call a method on another component that provides an essential application service like querying or updating the database with data from the request. We can use Spring to inject the database component into our request handler component, realizing the dependency between the two. If we add another component that uses the database for its task, like authentication, we can reuse the same data access component and lead Spring inject the dependencies as before. This demonstrates the power of Spring's IoC implementation. It's easy to create networks of specialized, reusable components. In fact, Spring dependency networks can get quite complex and large with specialized components that abstract common tasks. This programming pattern makes it natural to build reusable components, and the Spring framework has gathered a wide array of tools and modules under its umbrella.
2) What is Inversion of Control and Dependency Injection?
- Spring is an implementation of inversion of control, a popular enterprise design pattern. What it means is Most applications can be thought of as a collection of code components, some of which are business logic, meaning they contain the high-level steps of our application features, and some of which are persistent objects, with resources and state that have to be initialized and maintained carefully. In simple application programming, you might write all of the logic initializing these components and connecting them manually. But as those components become more complex and numerous, it becomes harder and more error-prone to do so for example in scenarios like does the database need to be initialized before the file cache? Where should you start the thread pool and the requestListener? How are you going to rewrite your code, connecting everything if something changes, like switching to a different database? Enter Inversion of Control.
- Inversion of control is a software design pattern, where you tell a framework what components need to exist, how to create them, and which components are needed by other components to function. Then, when you run the application, you're IoC framework of choice automatically initializes your components, and connects them, procedurally determining the best order and organization in which to do so. That means you can focus on designing robust, powerful components, and leave the connections between them to the framework. Changes to your design will be automatically factored into each deployment. Spring is our framework of choice for inversion of control, and it's provided to us in the form of dependency injection. What that means is that when we define a component, we also define its dependencies, other components that it needs to perform its function. Then, when we run our app, Spring ensures that all components are created in a compatible order, and inject dependencies in the components that declared them.
- Dependencies can be of any value, including everything from strings and numbers to database drivers and request handlers. Spring can recognize dependencies by Java type, component name, and configuration property. That means that Spring is incredibly flexible and powerful, and we can define any number of possible variable components as dependencies to be injected throughout our app. We can define constants, like service URLs and driver class names as dependencies, as well as the service classes that use them. We can define entire layers of business logic methods, as components that depend on resources like database drivers, that can be changed with a simple config file edit. This means that we can focus our development efforts on what matters, features, and business logic. Ultimately, the use of an IoC framework like Spring allows for structured application development, where the focus is on dividing the app's requirements into single-purpose components, that can be combined to implement features. It may be confusing at first, but once you get used to it, it's hard to imagine building an application any other way.
I would like to conclude the episode 3 of our Season 1 here, stay tuned for episode 4.
References
- https://www.udacity.com/course/java-developer-nanodegree--nd035
- Official Spring IoC Documentation
- Official Spring Annotation-Based Configuration Documentation
Disclaimer
This article is governed by the "Fair Use" doctrine and is only for the purpose such as criticism, comment & teaching.
- Note:- This is a living article.