SSH frameworks (struts, hibernate, and spring) that have recently become popular in the enterprise development field have been developed due to previous native servlet + JSP projects, so I will compare it with the native development mode during the learning process. Here I will summarize some of my understandings.
1. servlet + JSP Native Development
Let's briefly talk about the Java Web native development mode. Java Web development usually uses containers such as Tomcat. The Tomcat container implements a set of things, also known as the framework, that is, servlet. Tomcat runs on Web servers such as Apache. the Apache server is responsible for processing the HTTP protocol of user requests. After the HTTP protocol part of the user request is processed by the Apache server, Tomcat will process the content part. Tomcat sums up the common features of Web requests and encapsulates something called servlet. Based on servlet, developers only need to write the specific logic part for processing Web requests, as for the parsing of HTTP request fields, the encapsulation of response fields is handed over to servlet. In addition, JSP scripts are used to generate Web page splicing. The Tomcat container has a built-in parser that parses the JSP file into a servlet for execution. In addition to HTML, CSS, and JS content, JSP has some JSP tag syntax for servlet to pass some objects to JSP, in this way, the servlet processing results can be dynamically displayed on the JSP page to achieve the dynamic page effect.
2. Struts Framework
Let's look at some shortcomings of native development so that we can better understand why there is a Struts framework. In fact, from the perspective of function implementation, the general web development servlet can basically meet the needs, but when the project size is relatively large, it will appear relatively messy, because a large number of servlets are defined, each Servlet's processing process has some identical parts, and each servlet needs to be implemented repeatedly. To solve this problem, struts in the SSH framework is based on the idea of software hierarchy. It separates all parts of the web processing process and puts the same servlet processing logic into the framework for implementation, in this way, developers are released from repeat and focus on the implementation of specific business functions. Struts adopts the MVC design model. It divides Web applications into three parts: Database Access (M), business logic (C), and JSP page (c. You can use the xml configuration file to direct different actions to the corresponding action class for processing, and according to the results of action class processing, configure different results in XML to call different JSP page resources.
Struts also integrates some common work in Web development into the framework:
1) For international resource files, if the native development mode is used, strings in JSP must be implemented in multiple languages, and JSP string definition files in different JSP files should be introduced, if struts is used, it can be implemented by the framework and configured in XML.
2) JSP labels: struts defines some labels to enhance the JSP expression capability.
3) input type conversion. If native development is implemented by developers, Struts can be used to convert request parameters to built-in object members.
4) Input Validation. If it is native development, it is usually implemented by developers themselves. Using struts, you can define it through XML and implement it through the framework.
5) If struts is used, Struts takes over the whole process of processing Web requests, and users cannot insert necessary logic code freely during the process. Therefore, the Framework provides the interceptor mechanism to enable developers to insert some required processing logic during Web request processing, which is provided by defining the interceptor in XML.
From the above analysis, we can see that, compared with native development, Struts is not much enhanced in terms of functionality. The main job of Struts is to help us better Layer Web applications, at the same time, some repetitive work is integrated into the framework, which is the role of the framework.
3. Hibernate framework
Whether using the native development mode or the Struts framework, we need to implement the specific logic of the Web application by ourselves, including access to the database. For Web applications, the proportion of database access is relatively large, especially in front-end frameworks such as argularjs that have become popular in recent years, all of them put the business logic originally implemented on the backend to the front end for implementation. In this way, only database access and security-related functions are left in the backend work. If we adopt the native development mode, we need to implement crud access to the database by ourselves. We usually need to write SQL statements to directly access the database and then parse the SQL access results, and encapsulated in Java objects for upper-layer use. If you switch between different databases in the future, you need to rewrite the SQL processing code and adapt it.
The Hibernate framework encapsulates database access and provides us with another option. It completes database connection through xml configuration, maps database tables to Java objects, and maps the associations between tables to Java objects through xml configuration, in this way, developers only need to operate on Java objects when accessing the database, instead of implementing native development work on their own, so that they can be freed from specific database operations, later, if you switch to a database, you only need to modify the xml configuration. The disadvantage of this method is the addition of xml configuration.
4. Spring framework
The implementation of the Spring framework mainly includes two core mechanisms: IOC (dependency injection) and AOP (Aspect injection). Now I will analyze them separately.
IOC solves the dependency between objects. Java is an object-oriented language. Web applications define a large number of objects and objects are also correlated. In the general development mode, we need to program to create various Java objects and associate them with each other. The disadvantage of doing so is that there are many coupling between objects, you need to modify the code for subsequent maintenance changes. IOC provides another way to define the relationship between objects in XML format. Then, the Spring framework uses the Java reflection mechanism to help us create these objects, the objects are associated according to the definition, and then can be provided to the business logic for use. Some people may think about the best way to create objects and framework objects by themselves. For large projects, spring associated object creation and association may be clearer, however, the resulting cost is the expansion of XML configurations. Therefore, IOC injection is usually used for large component-level objects, and you can create and manage small objects by yourself, so as to strike a balance between the two.
AOP is a concrete implementation of an important idea in the Spring framework, that is, Aspect-Oriented Programming. Simply put, this idea is to insert some additional processing logic to an interface of an existing program. How does this happen? This depends on the IOC mechanism. Because IOC is used, the object is created by the Framework. When AOP defines the entry point and cut point functions in XML, it then defines the IOC objects pointed to by these entry points, then, when the framework creates these IOC objects, it will generate corresponding dynamic proxies for these IOC objects through the dynamic proxy mechanism of Java, and integrate the section functions in the defined entry point. When a user program calls the functions of the proxy object's entry point, it also automatically calls the cut-plane function, thus implementing the injection of the Cut-plane function. Through this mechanism, the independent plane functions can be injected into the IOC object to be cut in at will. In practice, a typical application is to add a transaction mechanism for the object, the ability to inject transactions into the objects required for the transactions independently, so that the transaction processing does not need to be closely coupled with the objects required for the transactions. In addition to transactions, some cross-cutting services in the web system are usually implemented using AOP, such as security check, cache, and Object pool management.
5. Summary
From the above analysis, Servlet helps us encapsulate the general processing logic of Web requests and responses, on the basis of servlet, SSH has helped us encapsulate the general processing logic of some web application systems. The software is formed by a layer-by-layer superposition, and abstract encapsulation is implemented in general scenarios. Of course, the encapsulation cost is a lot of xml configuration added, which is also the reason why many people criticized the complexity of SSH configuration. In actual projects, I want everyone to have a scale.
Understanding about SSH Framework Design