Benefits of using spring in the SSH framework

Source: Internet
Author: User

In the SSH box, spring acts as the management container. We all know that hibernate is used as a persistent layer because it encapsulates JDBC well, Program When interacting with the database, you do not need to write a large number of SQL statements. Struts is used for the application layer. It is responsible for calling the Serivce layer of the business logic. The SSH Framework process is roughly: JSP page ---- struts ------ Service (business logic processing class) --- hibernate (left to right)

Struts is responsible for controlling the Service (business logic processing class) and thus controlling the service lifecycle. In this way, the dependency between layers is strong and is coupled. In this case, the Spring framework is used to control act. The relationship between ion objects (in strus) and service classes is loose. Spring's IOC mechanism (control inversion and dependency injection) is used here.

Spring IoC (control inversion and dependency injection)
Control inversion: control the (dependency) Relationship between programs by containers. Code Direct Control
Dependency injection: The dependency between components is determined by the container at runtime, and the container dynamically injects a dependency into the component.
From the above we can easily see: from beginning to end act Ion only acts as a control tool for the service. He will not care about or ask how these specific business methods are implemented, he only needs to know the method interfaces provided by these business implementation classes. In the past, when the Struts framework was used independently, the lifecycle of all business method classes, and even some business processes were implemented by Act Ion. The coupling between layers is too tight, which not only reduces the efficiency of data access, but also makes the business logic look very complex and has a lot of code ., Spring container controls all act The life cycle of ion objects and business logic classes. Because the upper layer no longer controls the life cycle of the lower layer, the layer and layer are completely decoupled, so that the program running is more efficient and easy to maintain.

The second benefit of using spring (AOP application ):
Transaction processing:
In the past jdbctemplate, the transaction was committed successfully, and exception handling was completed through try/catch, while in spring. The spring container integrates transactiontemplate, which encapsulates all transaction processing functions, including complex business functions such as rollback of abnormal current events and data submission during successful operations. These are all managed by spring containers, which greatly reduces the amount of code for programmers and provides good management control over transactions. Hibernate also manages transactions. In hibernate, transaction management is completed by creating and maintaining sessions through sessionfactory. Spring also integrates sessionfactory configuration and does not need to set sessionafactory through hibernate. cfg. xml. In this way, the powerful transaction management functions of sping can be well utilized. This avoids getting session instances for each data operation to start transactions, commit transactions, and roll back transactions.
/Catch operation. These are the good applications of the AOP mechanism in spring. On the one hand, it makes the development business logic clearer and the professional division of labor easier. On the other hand, the application spirng AOP isolation Reduces Program coupling so that we can combine each aspect in different applications to greatly improve code reuse.

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.