Compare of Spring and SpringMVC - bernardopnunes/SoftwareEngineeringSDUW GitHub Wiki

Group

EDG

Index

  1. Spring
  1. SpringMVC
  1. Comparison
  2. Summary
  3. References
  4. Division of labor

Spring

introduction

Spring is an open source framework, which is created to solve the complexity of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows you to choose which component to use, while providing an integrated framework for J2EE application development.

Spring framework is a layered architecture consisting of seven defined modules. Spring module is built on top of the core container, which defines how beans are created, configured, and managed.

Each module that makes up the spring framework can exist independently or be implemented jointly with one or more other modules. The functions of each module are as follows:

Spring Core: the core container provides the basic functions of Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory pattern. BeanFactory uses the inversion of control (IOC) pattern to separate application configuration and dependency specifications from the actual application code.

Spring Context: Spring Context is a configuration file that provides context information to the Spring framework. Spring Context includes enterprise services such as JNDI, EJB, email, internationalization, validation, and scheduling capabilities.

Spring AOP: through the configuration management feature, Spring AOP module directly integrates aspect oriented programming functions into the Spring framework. Therefore, AOP can be easily enabled for any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in Spring based applications. By using Spring AOP, declarative transaction management can be integrated into the application without relying on EJB components.

Spring DAO: the JDBC DAO abstraction layer provides a meaningful exception hierarchy to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written, such as opening and closing connections. The JDBC oriented exceptions of Spring DAO follow the general DAO exception hierarchy.

Spring ORM: Spring framework inserts several ORM frameworks to provide object relational tools for ORM, including JDO, Hibernate, and iBatis SQL Map. All of these follow Spring's generic transaction and DAO exception hierarchies.

Spring Web: the Web context module is built on the application context module, providing context for web-based applications. Therefore, Spring framework supports integration with Jakarta Struts. Web module also simplifies the processing of multipart requests and binding request parameters to domain objects.

Spring MVC framework: MVC framework is a full-featured MVC implementation for building Web applications. Through the policy interface, MVC framework becomes highly configurable, and MVC contains a large number of view technologies, including JSP, Velocity, Tiles, iText and POI.

The functionality of the Spring framework can be used in any J2EE server, and most of the functionality can be applied in an unmanaged environment. The core point of Spring is to support reusable business and data access objects that are not bound to specific J2EE services. There is no doubt that such objects can be reused between different J2EE environments (Web or EJB), independent applications, and test environments.

advantages and disadvantages

Advantages

  1. It provides a method of managing objects, which can effectively organize the objects in the middle layer. A perfect frame "glue".
  2. Use Spring's IOC container to transfer the dependency between objects to Spring, reduce the coupling between components, and let us focus on application logic.
  3. The hierarchical structure is adopted, which can be added to the project incrementally.
  4. AOP is well supported, and convenient face-to-face programming is conducive to the cultivation of interface oriented programming habits.
  5. Spring is of low intrusion and low code pollution. The dependence of application on Spring API can be minimized.
  6. Provide good integration support for mainstream frameworks, such as Hibernate, Struts 2, JPA, etc.
  7. Spring DI mechanism reduces the complexity of business object replacement.
  8. Spring is highly open and does not rely on spring. Developers are free to choose part or all of Spring.

Disadvantages

  1. Interrupt the logic of the application program, making the code incomplete and not intuitive. At this time, only from the Source can not fully grasp all the behaviors of the application.
  2. Configure the logic that should have been coded, increasing the chance of error and additional burden.
  3. Time goes backwards and the support of IDE is lost. In the era of IDE's increasingly powerful functions, the past code refactoring and other headaches are more and more easy. Moreover, IDE also provides many powerful auxiliary functions, which reduces the threshold of programming. Generally speaking, it is much easier to maintain the code than to maintain the configuration file, or the mixture of configuration file and code.
  4. The debugging stage is not intuitive, and the corresponding stage of later bugs is not easy to judge the problem.

SpringMVC

  1. introduction

Spring MVC is a module of spring framework. Spring MVC and spring do not need to be integrated through the middle integration layer. Spring MVC is a web framework based on MVC. Spring MVC presentation layer: convenient for front and back end data transmission Spring MVC has a controller, which is similar to struts. It receives external requests and parses parameters to the service layer MVC refers to the design concept of C control layer, m module layer and V display layer. Spring MVC is the MVC framework in SSM framework, which helps (in a sense, it can also be understood as constraints) to develop web projects according to the design of MVC. Spring, the other two frameworks, is mainly used as IOC, For other design principles such as AOF, mybatis is used to operate the database conveniently, so they are all in the MV. As for V, it refers to the display part, and generally refers to JSP

  1. advantages and disadvantages

Advantage

Package code, low maintenance cost and low coupling In MVC mode, each layer performs its own functions, so once the requirements of one layer change, only the code in the corresponding layer needs to be changed without affecting the code in other layers. It is conducive to the division of labor in development and improves development efficiency In MVC mode, because the system is separated by layers, the division of labor in development can be better realized. Component reuse is conducive to code reuse and high reusability After layering, it is more conducive to the reuse of components.

Disadvantage

Not suitable for small, medium-sized applications Spending a lot of time Applying MVC to applications that are not very large often pays off. Increase the complexity of system structure and Implementation For a simple interface, strictly follow MVC to separate the model, view and controller, which will increase the complexity of the structure, and may produce too many update operations, reducing the operating efficiency. Too close connection between view and controller View and controller are separated from each other, but they are closely related components. View has no controller, and its application is very limited. View inefficient access to model data Depending on the model operation interface, views may need to be called multiple times to get enough display data. Unnecessary frequent access to unchanging data will also compromise operational performance.

Comparison

The creation of Spring container in contextloaderlistener is mainly used for some components that the whole web application needs to share, such as Dao, connectionfactory of database, etc.; while the container of SpringMVC created by dispatcher servlet is mainly used for some components related to the servlet, such as controller, viewresolver, etc. The relationship between them is as follows:


  1. Range The child container (SpringMVC container) can access the bean of the parent container (Spring container), and the parent container (Spring container) cannot access the bean of the child container (SpringMVC container). That is to say, when getBean is in SpringMVC container, if the corresponding bean cannot be found in its own container, it will go to the parent container, which also explains why the controller created by SpringMVC container can get the service component created by Spring container.

  1. Specific implementation In the specific implementation of Spring, both the child container and the parent container are put into the ServletContext through the setAttribute method of ServletContext. However, contextloaderlistener will create ApplicationContext before dispatcher servlet. When dispatcher servlet creates ApplicationContext, it will first find ApplicationContext created by contextloaderlistener, and then pass the latter ApplicationContext as a parameter to the setparent() method of ApplicationContext of dispatcher servlet. In other words, the creation of a child container depends on the creation of the parent container, which is created before the child container. In spring source code, you can FrameServlet.Java Find the following code in:
wac.setParent(parent);

Among them, WAC is the ApplicationContext created by disptcherservlet, while parent is the ApplicationContext created by contextloaderlistener. After that, the framework will call setAttribute () method of ServletContext to add WAC to ServletContext.


  1. Configuration of Spring container and SpringMVC container
  • Configuration of Spring container The Spring root container is responsible for the registration of all other non controller beans:
<context:component-scan base-package="cn.edu.tju.rico">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
  • Configuration of SpringMVC container SpringMVC is only responsible for the registration of controller related beans, where @controlleradvise is used to enhance the controller, and is often used to implement global exception handling classes:
<context:component-scan base-package="cn.edu.tju.rico" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

In general, Spring is the container framework of IOC and AOP. SpringMVC is a web framework added based on Spring functions. If you want to use SpringMVC, you must first rely on Spring. In a simple way, you can compare SpringMVC to struts. Spring is the container framework of IOC and AOP. SpringMVC is a web framework added based on Spring functions. If you want to use SpringMVC, you must first rely on Spring.

Summary

Spring is like an engine,and Spring MVC is an MVC framework based on Spring.Spring's two core AOP and IOC can be used independently for any application, including integration with MVC frameworks such as struts and ORM frameworks such as hibernate. At present, many companies use spring + struts (2) + Hibernate as their so-called lightweight development. Spring MVC is an MVC framework. Spring MVC annotation type development is more convenient than struts 2, and can directly replace the above struts (of course, as a very mature MVC, the function of struts is better than spring, but spring MVC is enough). Of course, the execution efficiency of spring MVC is higher than that of struts, because the value stack of struts affects the efficiency.

References

https://www.cnblogs.com/rainbow70626/p/9784938.html https://blog.csdn.net/qq_35251019/article/details/74963347 https://www.cnblogs.com/hafiz/p/5875740.html

Division of labor

  • Chen Wang(@zhaoboy02)->Spring
  • Mengdian Xie(@Mengdian-Xie)->SpringMVC
  • Yimin Zhang(@xiaozhuzhu2333)->Comparison
  • Junyi Li(@lijunyi3399)->Summary
  • Yizhao Yang and Mengjie Liu(@liumengjie0119)->Poster
  • Zhao Xu and Rui Zhang->Presentation
⚠️ **GitHub.com Fallback** ⚠️