Introduction To Spring Boot MVC
Spring is one of the most popular frameworks in the industry today, and its project, Spring MVC, is being widely adopted to develop web applications.
Spring MVC follows the basic conventions of Java and Spring, and can help you to develop applications quickly and easily, because it does a lot of work for you already, and allows you to focus on the business logic of your application.
In this article I’ll know what is Spring MVC and Why we use it? . Also we’ll show some features of Spring MVC .
What is Spring MVC and Why we use it?
What is Spring MVC
Spring MVC : It is a web-based framework for doing web application development. It is one of the projects of the Spring Framework. As the name suggests, it is based on MVC design pattern, which is Model, View, and Controller.
So, how does that work? So, let’s say you have a view where the request comes from. That request is going to be intercepted by a component called controller. The controller is going to do the necessary application logic, and then there’s model, which will hold the data of all your application flows. This data from the model can then be displayed back as response to the browser, which is the view, again.
So, when you talk about Spring MVC, these definitions are pretty much the same. Views are JavaServer pages, JSPs, where you’re going to have the content displayed in the browser. Then you have models, which are nothing but domain objects, or JavaBeans, which will hold the data of your application. And you have the controllers, which are going to receive your HTTP requests and forward the control to the next component in line.
Why Spring MVC ?
It’s a very important question. First of all, Spring MVC is, as I said, one of the projects of the Spring Framework, which means it will work on the basic principles that Spring works on.
The most crucial feature is inversion of control, or as we say, IOC. What this means is, let’s say if you create a controller class in Spring MVC then it is going to be created, instantiated inside a Spring context container, and it will be available for the entire run-time of the application. So, all the beans are managed by the Spring context.
Similarly, any other concept like aspect-oriented program, or the fact that Spring works with JPA, Java Persistence API. All of these principles can be seamlessly integrated with Spring MVC because it is part of the same umbrella.
Spring MVC provides a very high degree of flexibility and adaptability during your development process. Let’s say you’re writing a controller class, the method that you write in it, the signature, can be decided by you. It is not bound by the rules of the Spring MVC framework, that way it’s very flexible. The only thing is that the portions of that method signature have to annotated correctly with appropriate annotations, so that the MVC framework recognizes the apropos.
This framework promotes high reusability of business logic. Let’s say you have existing JavaBeans, or form objects of backing beans that you’ve carried from your Legacy applications. There is no need that these classes should extend or inherent classes and interfaces from the framework. They can be very easily plugged in to the Spring MVC application. That way it promotes reusability.
It is very mature and highly stable with loads of support online. You have a fantastic documentation that’s available for Spring, for this framework as well, Spring MVC. You have a lot of get-up projects, and you also have a lot of beautiful online forums where you can post your questions when you’re stuck up with your development process.
Features of Spring MVC
- Firstly, it is built on top of the Servlet JSP API, which is from the Java Enterprise space, which essentially means that all the concepts of HTTP request, response, servlets, filters, listeners, Java Server Pages, Java Server Pages Tag Library, all of those are pretty much the same that we’ve already worked on if we have worked on Servlet API before. Spring MVC has put a layer on top of it, of course, with more optimization.
- Spring always says one line. If you follow the basic Java and Spring convention, then it will be very easy to build a minimum viable product with Spring MVC framework. You do not have to stuff in a lot of configuration for it to be working in the initial startup.
- When you talk about Spring MVC, we have said, of course, that it is based on Model-View-Controller design pattern and each of these components has a very clear distinction of responsibilities. So all of these components are independent of each other, none of them know what the other does, which means there’s a clear separation of concerns and hence an increased maintainability of the code.
- Spring MVC offers you a plain old Java object, which is POJO based, light weight development. We’ve spoken before that the classes that you write in Spring MVC need not extend or implement classes and interfaces, which means it is not tightly bound by the framework. It also means that you will have a freedom to go and extend some other API classes, which may be crucial for your application logic.
- High level of possible customization that you can possibly do with Spring MVC. For example:
How are your URLs going to be mapped?
How are your teams supposed to be resolved?
How is locale resolution done?
How are validations carried out?
What kind of messages do you want to have for your validations?
All of these features can be very easily customized with the help of this Spring MVC framework.
The point that I have on my slide right now is very important.
- Spring MVC never couples tightly with a particular view technology. Let’s say today you’re working on Java Server Pages, but you need not stick to it. There are other templating engines, for example, Velocity, Freemarker, Thymeleaf, and all these kind of templating pages that you can possibly integrate in your application. If you are not satisfied with either of them, you also have an option to write your own views by implementing the view interface inside the Spring MVC framework.
- Not only does Spring MVC provide you a bunch of HTTP endpoints, but it provides you a way to develop RESTful web services, which is such a common thing in the industry today. If you are working on a product, you often have this very common requirement of opening up a host of RESTful endpoints for your clients accessing data in various ways.
Right, so that’s not the exhaustive list of features that we have in Spring MVC. There are many more. But if you want to have a basic startup that’s required to work with this framework, this is the list of features that you definitely need to know. Alternately, just remember that Spring MVC, sometimes referred to as Spring Web MVC, that’s because the source code has been labeled like that. But you can rest assured that both of them are exactly the same.
Spring MVC architecture
Let us take a look at the Spring MVC architecture at a high level.
- Let’s say this is your client, a JSP. It could be any other view technology that you want to work with.
- When you have an incoming request from your client it is intercepted by an API called Dispatcher Servlet, and this is the star of the Spring MVC framework.
- Every request is always uniquely identified by a urlpattern. And this urlpattern is mapped to a filter and a controller dedicated to that request processing. This url mapping resolution will be done by Dispatcher Servlet by means of interacting with another special bean type called Handler Mapping.
- Once it does this resolution, then it’ll delegate the control to the respective controller component. Dispatcher Servlet works on a design pattern called Front Controller. This pattern is common even across the other web MVC frameworks. How does it work? It’s like a traffic policeman. All the vehicle at a traffic signal comes towards the policeman first and then stops. Then on the basis of who wants to go where the policeman will divert that traffic at all directions. This is how
- Dispatcher Servlet works. All the incoming requests that come in the application are first intercepted by it and bases who wants to go where it’ll delegate the control to the respective controller.
- The controller is then free to call the rest of the Application Business Logic where it may talk to database or any other third-party service.
- Once the logic execution is completed, the flow comes back to the controller. And that’s the point where the controller decides the name of the view that the control is supposed to be for, what it do for the display of data. Now this is a logical name. This logical name is picked up by the Dispatcher Servlet and it consults another component called View resolvers. View resolvers will help Dispatcher Servlet know the mapping of the logical name of the view to the respective JSP template. Thereafter the Dispatcher Servlet is then going to carry the JSP and display it in the browser as part of the response.
Thank you very much for joining me on this Spring MVC article . Now that you have got a good start with an overview of Spring MVC, you can explore and start migrating your legacy applications to this framework.
It will also be easier to understand any other web MVC frameworks that you may wish to work with.
Additionally, you can look up the Spring MVC documentation, look up on Github for many interesting projects on Spring, and also post your questions on Stack Overflow to get some easy answers.
Resources :
- Lynda.com
Comments