One in-depth analysis of spring MVC Framework-overall analysis

Source: Internet
Author: User

In today's MVC Framework, it seems that webwork2 has gradually become the mainstream, and the combination of webwork2 + springframework has become increasingly popular. This seems to mean that the MVC framework provided by spring is far worse than webwork2, so we use webwork2 in succession. Indeed, spring's MVC framework is not the core component of spring, but its power exceeds the imagination of many people. Many people, including xiecc, think that spring's MVC framework is excellent, or even better than webwork2.
The following lists some important decisions made by spring MVC framework during design, and compares the MVC frameworks related to the sum, such as webwork2 or struts:

I. The entire MVC configuration of spring is based on the IOC container
Compared with Struts or webwork2, this is a strange decision in ms. Let's take a look at the spring MVC configuration file. What we first see is not action or form, but some beans with specific names, bean configurations are simple or complex attributes. What we see is the easier data structure for machines, rather than the easier-to-understand elements.
But this is exactly the powerful source of spring MVC! Because its configuration is the configuration of the core IOC container of spring, it means that the power of all IOC containers can be presented here, and we can expand and enhance spring MVC as we like, we can complete many unimaginable tasks in other MVC framwork. Do you want to extend the new URL ing method? Do you want to change the themeresolver or localreolver implementation? I want to display a new type of view on the page (for example, RDF, haha, a little secret: xiecc is a semantic web study. Even though it is a daily task, it does not write papers, it only writes gossip )? Even want to define AOP directly in the controller? These are a piece of cake for spring MVC.
I have not carefully studied the extension mechanism of webwork2. I know that through the interceptor mechanism of webwork2, many extensions can be performed, and even a simple IOC container can be created. But no matter how powerful it is, how many extensions it provides. Its power is hard to compare with the real IOC container. The plug-in function of Struts is famous for its abuse, although it also provides the plug-in mechanism.
Another reason why spring uses IOC configuration is that it makes it very easy to integrate Spring MVC with spring's IOC container. Spring provides integration with Struts and webwork2, but such integration requires indirect packaging, and it is not natural. It also leads to multiple configuration concepts. webwork2 needs to configure bean in spring and then configure its own xwork file. Imagine that our bean is directly a controller, which can directly complete all MVC tasks.
Another reason rod Johnson uses the IOC container is that it reduces a lot of development workload. Let's take a look at urlmapping. The property provided by urlmapping itself is a hashmap. The data in our Bean naturally exists only after the configuration is complete. Haha, it's so nice. You don't need to parse XML like struts, and read its content into hashmap one by one.
Although such a configuration may be a bit weird, if we are very familiar with spring's IOC container, we will find it very friendly and simple.
The last is a simple secret. How does spring know that the configuration of a bean is urlmapping? The configuration of another bean is viewresolver? In fact, it is very easy to read all beans into the memory, and then find them by bean name or type. Finding by name is a simple getbean method, and using the beanfactoryutils. beansoftypeincludingancestors Static Method for Finding by type.

2. Spring provides a clear concept of model, view, and corresponding data structure.
In spring, there is an interesting data type called modelandview, which simply encapsulates the data to be displayed and the results to be displayed in a class. However, it provides a clear MVC concept, especially the enhancement of the model concept, which makes the logic of the program clearer.
Remember that when I used to write a program in struts, I often put things in httpsession or httpservletrequest (or set them to form, although not very useful) to display data ), this results in the fuzzy model concept and the tight coupling between struts and JSP pages. If we want to replace it with veloctiy, we have to add another plugin, because the data in velocity does not need to be put in the request.
Webwork2 emphasizes decoupling from the Web Framework and the simplicity of its command mode. Therefore, there is only a simple get or set method in its action. If data is returned, it simply returns a string. Of course, this implementation has its advantages, but it diluted the concepts of model and view. Rod Johnson believes that the action in webwork2 contains both the role of action and model. Such a class has too many responsibilities and is not a good design. Of course, Jason Carreira does not agree with this idea, because the model object in the action can be delege to other objects. However, in any case, the root cause of this debate is that webwork2 has deprecated the concept of model, view, and even web. The benevolent sees benevolence, the wise sees wisdom, and the final result is still like my personal liking.

3. The controller of spring is Singleton, or the thread is not secure.
Like struts, spring's controller is Singleton, which means that the system will process each request with the original Instance, resulting in two results: we do not need to create a controller every time, this reduces the time for object creation and garbage collection. Because there is only one controller instance, when multiple threads call it, the instance variable in it is not thread-safe.
This is what webwork2 boast. Every action of webwork2 is thread-safe. Because every time a request is sent, it creates an action object. Because the efficiency of the modern JDK garbage collection function is no longer a problem, this mode of throwing away an object after it is created has also been recognized by many people. Rod Johnson even used this as an example to prove that the object pool function provided by J2EE is of little value.
But when people boast about how thread security is important, how many people need to consider thread security? Rod Johnson also raised other questions when analyzing ejbs. Instead of having no EJB thread security magic, the world will perish. In most cases, we do not need to consider thread security or the object pool at all. Because we do not need to maintain the instance status in most cases.
At least I have written so many struts actions and so many spring controllers, and almost never encountered the need to maintain the status of the instance variable. Of course, I may not write enough code. The struts designer Craig R. mcclanahan once said that he had two immature conditions when designing struts: there was no concept of test-driven development at the time; at that time, JVM's garbage collection performance was too high. If it is re-designed now, it will also use the design method of generating a new object for each request, which can solve the thread security problem.

4. Spring does not hide servlet-related elements like httpservletrequest or httpservletresponse like webwork2 or tapestry.
This is an important design decision. In webwork2, we do not have httpservletrequest or httpservletresponse, but only data in getter, setter, or actioncontext. Such a result leads to a clean action, which is totally irrelevant to the Web, A bean that can run independently in any environment. So what does a command-based action of webwork2 bring to us? I think there are two main points:
1. It enables our actions to be easily tested.
2. You can add business logic in the action and reuse it by other classes.
However, by comparing them with spring, we will find that the benefits of these two features are not as obvious as we think. The Controller class of spring can also be easily tested. Let's take a look at the packages under spring-Mock. It provides mockhttpservletrequest, mockhttpservletresponse, and other classes to make the Controller Test abnormal and easy. Let's take a look at the business logic in action. Jason Carreira once said that we can add business logic to webwork2's action, because action is not dependent on the web. But how many people actually add business logic to the action? Most people will delegate the business logic to another service class or manager class. It is clear that adding business logic to actions makes the layered architecture of the entire system unclear. In any case, the web layer is the web layer, and the business layer is the business layer, mixing the logic of the two will always cause problems. In addition, adding the business logic to the action will use this action class to become huge. The action of webwork2 creates an instance for each request, although the performance impact is not great, however, it does not mean that the business logic should be updated every time. In most cases, the business logic should be single-instance.
If you do not display the request and response to the user, it will certainly cause functional losses. In general, it may be sufficient to use the interfaces provided by webwork2, but sometimes we have to know the request and response to exert more power. For example, in my previous project, there was a page with a tree structure dynamically generated through recursion. It was painful or impossible to display recursion on the JSP page. Therefore, I used response to write the page directly, this is very easy in spring, but it may be difficult in webwork (I am not sure, even if I have not studied deep enough, maybe the experts have a way ).

5. Spring provides a good but inadequate interceptor mechanism
Looking back at struts, it doesn't even provide us with Hook Point opportunities in the architecture, and we don't have any chance to join our interceptor. We can only perform a limited number of extensions by reloading the struts requestprocessor class.
At webwork2, it seems that interceptor has become the core of the entire framework. Except for the core components of action, everything else is interceptor. Its powerful interceptor feature makes it very convenient to extend the entire architecture. Some people call this interceptor as AOP, and Jason Carreira proudly claims this is called pragamtic AOP. I don't agree that this is AOP. It is just a simple interceptor mechanism. But in any case, its interceptor does have powerful functions.
Spring also provides its interceptor mechanism. Its handlerinterceptor has three interceptor Methods: pehandle, posthandle, and aftercompletion. Before the controller is executed, the controller is executed and after page render. Although it is sufficient in most cases, it is not as powerful as webwork2 in terms of functionality. From the perspective of AOP, it does not provide the und interceptor, but only before and after interceptor. This means that we cannot maintain the status before and after Interceptor. In the simplest case, if we want to calculate the execution time of a controller, we must keep the begintime status after executing before, after, we call it out, but obviously this status remains a problem. We cannot put it in the instance variable because interceptor is NOT thread-safe. This problem may be solved through threadlocal, but such a simple function requires such a method to handle it. Obviously, this interceptor is still designed to be somewhat problematic.
6. Spring provides multiactioncontroller so that it can contain multiple actions in a class.
This design is similar to struts's dispatchaction, but it provides a more flexible mechanism. When our project grows, it is worthwhile to put a function-like method into the same action! Webwork2 lacks such a mechanism. If you look at the source code of spring, you will find that the workload for implementing multiactioncontroller is quite small, but you just need to use the reflection mechanism to execute the parsed method name. In fact, webwork2 can also provide such a mechanism. Although it is not very elegant in design, it is indeed very useful.

7. Spring provides more options
Let's look at the Controller provided in spring. It provides many different controller classes. Do you want to generate a wizard? Is the Controller dedicated for submitting forms? Do you want to hold classes of multiple methods? Spring provides a wide range of sub-classes to extend these options. Of course, we can easily expand these features on our own.
Let's take a look at Spring's viewresolver, which provides countless different types of viewresolver. More importantly, we customize our page ing method. Take a look at strtus and look at webwork2. There will be an indirect conversion between the page and forward name. We must configure a string in the configuration file (typically success) that corresponds to that page. However, we have a higher degree of freedom in spring. We can adopt the webwork2 policy or a simpler policy, such as removing the JSP file name from the extension ing method. Some people may think this ing method is naive, but I think it is very useful, even in large projects.
Are there any new extensions? Let's take a look at Spring web flow, which is a Child Project of Spring framework. It provides configurable implementation for a long string of page stream-based wizard pages. In spring 1.3, it will be part of Spring framework.

VIII. Spring tag
Although the number of tags in spring is small, it is carefully designed. The goal is simple: Enable the artist to easily edit pages. Because the text on the spring page is still text, and the checkbox is still checkbox, unlike the tag in struts or webwork2. It only uses springbind to wrap the input content. Therefore, although the page shows more code than webwork2, it is definitely valuable.

In the following chapters, I will analyze how spring enables our web applications to access the IOC container without knowing applicationcontext, then, a simple source code analysis will be performed on the design and execution process of spring, and several methods to expand spring MVC will be provided.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.