The call sequence of service, action, jsp, formbeam, and dao In the ssh framework

Source: Internet
Author: User

Sp initiates a request. Actionform encapsulates request parameters. Action accepts the request and receives the encapsulated actionfromaction to call the service. After the service is processed by the business logic, it then calls DAO (or directly implements the CRUD operation on data in the service) DAO to perform CRUD on the database. 1. dao corresponds to service. In general, Hibernate DAO only operates on one POJO (simple Java object, which is actually a common JavaBeans) object. Therefore, a DAO corresponds to one POJO object. The Service layer manages transactions (declarative transactions) to process multiple POJO objects (data operations on multiple tables ). The Service layer (its interface implementation class) is injected with multiple DAO objects to complete data operations. 2. My opinion on whether the Service is available may not be correct. My mind now has two models for building the business layer: Mode 1 is Service + DAO, that is, in DAO, only CRUD and similar simple operations (called function points, excluding business logic) are performed. In Service, one or more DAO Function Points are called to form the business logic. the number of services should be determined by the function module. In this model, the business logic is placed in the Service, and the transaction boundary should also be controlled in the Service. of course, controlling transactions directly in the Service will introduce non-business logic code. Fortunately, Spring AOP can solve this problem, which is also one of the reasons for introducing Spring. if the disadvantage is that the operations on some objects are simple CRUD, and the Service layer is cumbersome. mode 2 is Service + BO, while BO = DAO + business method adds business methods based on the original DAO to form BO objects. It should be noted that the business methods in BO usually target an object. If you need to span Multiple object objects, the methods should be placed in the Service. For example, a simple bank account management system creates a BO object for an account, which can contain business methods such as password modification and money collection (it is not difficult to see that, these methods only operate on a single account object ). Now you need to add a transfer method and put it in the Service. What is the relationship between Service and BO? Another example: Taking the state administration as an example: the grain Bureau is responsible for collecting food and selling seeds, and the Ministry of Construction is responsible for approving land sales and constructing highways. This is part of the administration. Suddenly a flood occurs somewhere, and the grain Bureau needs to open a warehouse for food relief. How can the Ministry of Construction coordinate the two Departments to build a temporary house? It is necessary to set up a special Disaster Relief Committee to allocate resources in two parts. Here, the two parts are BO, and the relief board is Service. I don't know if the expression is correct. When dividing services and DAO in Mode 1, the boundary is clear, but there will be unnecessary code. The division of Mode 2 is relatively complex, but it can improve the coding efficiency. Of course, in small-scale applications, if there is no Service, DAO or BO is acceptable. 3. Whether or not the Service and DAO interfaces have interfaces is a contract, which can be implemented in multiple ways. Therefore, whether the interface is available depends on whether the specific implementation needs to be diversified. If there is only one implementation for a DAO or Service, it is of little significance to abstract the interface. However, some large applications may require multiple implementations of DAO and Service (for example, the account DAO in the preceding example may require a Hibernate implementation, a CMP implementation, and a JDO implementation ), an interface is required to hide a specific implementation class at the upper layer. There are two ways to hide the creation process of a specific implementation class: one is the practical factory method, the cost is a large amount of code (each DAO and Service is a factory ). Second, use Spring IoC to implement dependency injection without writing additional code. This is also the second reason for introducing Spring. DAO layer: the DAO layer is mainly used for data persistence layer. Tasks responsible for communicating with the database are encapsulated here. The DAO layer is designed first to design the DAO interface, then define the implementation class of this interface in the Spring configuration file, and then you can call this interface in the module to process data services, instead of worrying about the specific implementation class of this interface, the structure is very clear. The data source configuration at the DAO layer and the database connection parameters are all configured in the Spring configuration file. Service layer: the Service layer is mainly responsible for the logic application design of the business module. The same is to first design the interface, then design its implemented class, and then configure its implementation Association in the Spring configuration file. In this way, we can call the Service interface in the application for business processing. The Service implementation at the Service layer must call the interface at the defined DAO layer. Encapsulating the business logic at the Service layer is conducive to the independence and reuse of the general business logic, and the program is very concise. Controller layer: the Controller layer is responsible for controlling specific business module processes. In this layer, you must call interfaces at the Serice layer to control business processes, the control configuration is also carried out in the Spring configuration file. For specific business processes, there will be different controllers. In the specific design process, we can abstract the process, design a reusable sub-unit flow module, which not only makes the program structure clearer, but also greatly reduces the amount of code. The View layer is closely integrated with the control layer, and the two need to be combined to cooperate with the development. The View layer is mainly responsible for the presentation of front-end jsp pages. The DAO layer and Service layer can be developed independently, and their coupling degree is very low, this mode is particularly advantageous in the process of developing large projects. The Controller and View layers must be developed together because of their high coupling, but it can also be seen as a whole independent of the first two layers for development. In this way, before the layers, we only need to know the interface definition and call the interface to complete the required logical unit application. Everything is very clear and simple. The overall planning requirements for the DAO design at www.2cto.com correspond to the design tables and implementation classes one by one. The methods in the interfaces defined by the DAO layer are similar, which is determined by the database access operations in the DAO layer. For database operations, we basically need to use new operations, update, delete, query, and other methods. Therefore, the Operations corresponding to these methods should be basically covered in the DAO layer. In addition, you can customize some special database access methods. The Service logic layer is designed to build the Service layer on the DAO layer. After the DAO layer is established, the Service layer can be created, and the Service layer is under the Controller layer, therefore, the Service layer should call both the DAO layer interface and provide the interface to the Controller layer class for calling. It is just in the middle layer. Each model has a Service interface, and each interface encapsulates its own business processing methods. Some methods defined in the DAO layer are not used in the Service layer. Why should they be defined in the DAO layer? This is determined by the requirement logic we define. The operations on the DAO layer are basically generic after being abstracted. Therefore, when defining the DAO layer, we can define the relevant methods, the advantage is that you do not need to modify the DAO layer when extending the Service, which improves the scalability of the program.

Related Article

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.