Home  >  Article  >  Java  >  What is the use of java framework

What is the use of java framework

(*-*)浩
(*-*)浩Original
2019-05-31 10:14:583257browse

Java framework, simply understood, is a reusable design component. It stipulates the application architecture, clarifies the dependencies, responsibility distribution and control process between the entire design and collaborative components, and is expressed as a A method of collaboration between a group abstract class and its instances, which provides a context relationship for component reuse.

What is the use of java framework

What are the commonly used Java frameworks

Struts, Hibernate and Spring are the ones we use in Java development Commonly used frameworks, they provide the most appropriate solutions for different application scenarios. But do you know how these well-known frameworks originally came into being?

We know that traditional Java Web applications are implemented using JSP Servlet Javabeans. This model implements the most basic MVC Layered, the program structure is divided into several layers, including JSP responsible for front-end display, Servlet responsible for process logic control, and Javabean responsible for data encapsulation

But this structure still has problems: such as JSP The page needs to use symbols to embed a lot of Java code, resulting in a confusing page structure. Servlets and Javabeans are responsible for a lot of jumps and operations, tight coupling, low program reusability, etc.

In order to solve these problems, the Struts framework appeared. It is a perfect MVC implementation. It has a central control class (a Servlet). For different businesses, we need an Action class to be responsible for the page. For jumps and background logic operations, one or several JSP pages are responsible for data input and output display, and a Form class is responsible for transferring data between Action and JSP. A set of tags provided by the Struts framework can be used in JSP, which is as simple as using HTML tags, but can complete very complex logic. From now on, there is no need to have a line of surrounding Java code in the JSP page.

However, placing all the operation logic in the Action of Struts will make the Action class less reusable and the logic confusing. Therefore, people usually divide the entire Web application into three layers. Struts is responsible for the display layer. The business layer is called to complete the operation logic, and the business layer then calls the persistence layer to complete the reading and writing of the database.

Use JDBC connection to read and write the database. The most common thing we do is to open the database connection, use complex SQL statements to read and write, and close the connection. The obtained data needs to be converted or encapsulated and then transmitted to the outside. This is a Very cumbersome process.

At this time, the Hibernate framework appeared, which requires you to create a series of persistence classes. The attributes of each class can be simply regarded as one-to-one correspondence with the attributes of a database table. Of course, it can also be implemented Correspondence of various table associations in relational databases. When we need related operations, we no longer need to pay attention to the database table. We no longer need to query the database line by line. We only need the persistence class to complete the functions of addition, deletion, modification and query. Make our software development truly object-oriented rather than messy code-oriented. My feeling is that using Hibernate reduces the amount of programming by 80% compared to JDBC.

Now we have three layers, but what are the calls between each layer? For example, if Struts in the display layer needs to call a business class, it needs to create a new business class and then use it; the business layer needs To call the persistence layer class, you also need to create a new persistence layer class. Calling each other through this new method is the embodiment of the worst design in software development. To put it simply, the caller depends on the callee, and a strong coupling is formed between them. If I want to reuse a class elsewhere, other classes that this class depends on also need to be included. The program becomes very confusing, each class depends on each other and calls each other, and the degree of reusability is extremely low. If a class is modified, many classes that depend on it will be affected. For this purpose, the Spring framework appears.

The function of Spring is to completely decouple the dependencies between classes. If a class depends on something, it is an interface. As for how to implement this interface, it doesn't matter. As long as you get a class that implements this interface, you can easily inject the implementation class into the class that calls the interface through the xml configuration file. This dependency between all classes is completely replaced by configuration files. So the core of the Spring framework is the so-called dependency injection and control inversion.

The current structure is that Struts is responsible for the display layer, Hibernate is responsible for the persistence layer, and Spring is responsible for the middle business layer. This structure is currently the most popular Java Web application architecture in China. In addition, because Spring uses dependency injection and AOP (aspect-oriented programming), its internal model is so excellent that Spring itself has also implemented an MVC framework that uses dependency injection, called Spring MVC. At the same time, for good To handle things, Spring integrates Hibernate, which elevates transaction management from Hibernate's persistence layer to the business layer, making it more convenient and powerful to use.

The above is the detailed content of What is the use of java framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn