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

What is the use of java framework?

藏色散人
藏色散人Original
2019-05-22 13:12:465936browse

Nowadays, frameworks are used in projects. A very common framework now is

SSH(Struts+SpringMVC+Spring+Hibernate),SSM(Struts/springMVC+Spring+Hibernate)

. In my recent project itoo, the framework used is easyUI SpringMVC EJB hibernate. The purpose of layering projects is for better Decoupling and layering can be solved without your framework at all. Why don’t you need so many frameworks?

What is the use of java framework?

And now the technical framework of the control layer of many web projects in the company Migrating from struts2 to springMVC, why do we choose frameworks like struts2 or springMVC instead of using technologies like servlet plus jsp when doing web development in Java?

Especially now that the front-end pages of our web are developed using template languages ​​​​such as velocity, abandoning jsp, what kind of benefits will such a choice bring to our java web development? Following this question With this idea, I found a new question, why do many Java enterprise-level developers now choose the spring framework? What does the spring framework bring to the applications we develop?

After thinking about it so deeply, I really didn’t quite understand it. It seemed difficult for me to find a completely convincing answer. In the end, I found that these technologies that I thought I was familiar with and commonly used were actually not. There are still many things that are strange and confusing to me. These strange and confusing things are also the key to whether I can use them at a higher level. However, after so many years of accumulation, technologies such as struts2 and spring have become quite large and complex. , they have a wide range of applications. Although I have used them for a long time, there are still many technologies that I am not familiar with and unclear about.

There is a very general benefit of using frameworks: First, it can speed up During the development process, reusing code in similar projects will save developers a lot of time and energy. The framework provides pre-built modules for performing tedious coding tasks.

This benefit applies to any framework Very applicable. But what if you think about it more deeply?

In fact, there are many excellent frameworks in software. There is a type of framework whose characteristic is that it is based on an existing technology. , a technical framework that provides the same business functions as the existing technology. This new technical framework is easier to use, more robust and more powerful than the original technology.

For example: jQuery, as well as struts2 and springMVC to be discussed in this article. These frameworks are quite complicated, but they have only one advantage: they allow users to only care about the development of core business, and the framework helps You shield all kinds of technical issues that have nothing to do with the original technology and business development. The reason why frameworks like jQuery, struts2 or springMVC are excellent is that they do this so well that many programmers who use it no longer know the true nature of the original technology, so we need to understand struts2 Better, more proficient and in-depth use. Here we have to jump out of struts2 technology, go to servlet, the source of struts2 technology, and carefully study the characteristics of servlet. Only in this way can we learn the struts2 framework better.

The function of Servlet is to receive the request from the browser to the server and return the processed response from the server to the user's browser. The browser and the server communicate through the http protocol. To communicate, the process is that the browser assembles the requested http message according to the user's selection and the relevant information according to the specifications of the http protocol message. The message is transmitted to the designated server through the network, and the server receives the message information through a specific web container. .

For example: web containers such as tomcat, jetty, and jboss. The web container will parse the http message. If it is a user request, the final parsed message information will be stored in a request object. The server After using this request to complete the corresponding processing, the server program encapsulates the result information into the response object, and then hands the response object to the web container. The web container converts the response object into a message of the http protocol and sends the message It is passed back to the browser, and the browser finally parses the response message and displays the final result to the user.

The Web container created the servlet interface. The servlet interface is where developers implement their own business logic. Programmers developing servlets are like doing fill-in-the-blank questions, and the context or context prompt of the fill-in-the-blank question is composed of request and response. Object, but the servlet interface in the JavaEE specification is very simple, with only three methods init, service and destroy. However, this interface is too general, so the specification also provides an HttpServlet class, which provides doGet, doPost, etc. according to the http request type. Method, the biggest feature of the servlet interface is that it is defined according to the characteristics of the http protocol. Therefore, if the user is not particularly familiar with the characteristics of the http protocol when developing servlets, he will encounter more or less confusing problems, especially when encountering some For complex and special requests:

For example, file upload returns a special file format to the browser. At this time, it is not very convenient to use servlet development. There is another problem in servlet development that may be often overlooked, which is the type conversion of the requested data and HTTP protocol transmission. They are all in text form, and they are also text types after being parsed by the web container. If we encounter types such as currencies, numbers, and dates, we need to convert them according to the actual situation. If the page transmits a lot of information, we have to do a lot of type conversions. This kind of work has no technical content, is manual labor and can easily lead to program errors.

At the same time, the enterprise development of Java is all carried out around JavaBeans, and the type-converted data must be encapsulated into the corresponding JavaBeans. This kind of going around is definitely not a good thing for project development, so the ancient Struts1 found a solution to this problem, which is to define a DTO object (data transfer object), which is specifically responsible for doing such things. However, in struts2, the entire action that replaces the servlet itself is a javabean.

One of the technical features of Java enterprise development is the use of javabeans. One of the characteristics of struts2 is that the operation class it replaces servlet is a typical javabean. First, the struts2 framework converts and encapsulates the data transmitted by the page. Finally, the request information is encapsulated into the properties of this javabean, so that we can save the troublesome type conversion and encapsulation problems when developing web programs. Earlier I mentioned that traditional servlets are defined according to the http protocol, and it will be based on your Request method (post or get method) to handle user requests.

But for a program developer, a request, specifically a URL, is actually a function provided by the server to the outside world, or an external action by the server side. If When we use servlet to develop programs, we have to convert http actions into specific business actions, which makes program development cumbersome and increases the difficulty of development. Therefore, struts2 replaces servlet's javabean and blocks the http request method and Regarding the conversion of specific business actions, every method in Javabean can correspond to each URL request one-to-one, which will inevitably reduce the difficulty of development.

Another function of Servlet is to construct a response object so that the page can obtain the correct response. In fact, a modern browser is a multimedia tool. Text, pictures, videos, etc. can be displayed in the browser. Resources Differences will lead to differences in http response messages. If we use servlet development, we must use hard-coded processing in the Java program according to the different resources. Such a program is difficult to reuse, and if the programmer has a certain resource requirement, Insufficient processing and understanding will lead to problems. Struts2 separates this logic from the Java program in the form of configuration files and uses configuration methods for unified management. This approach is similar to spring's AOP method, so that the results The processing method is more unified and easier to manage. It also improves the robustness of the program and reduces the difficulty of development.

Servlet is the C layer in the MVC development model, which is the control layer. The control layer is like the Russian double-headed eagle (one head looks east and the other looks west), and the other head looks towards the M layer model layer. Look, look at the view layer of the V layer. The model layer is also written in Java, and the control layer is also developed in a server-side language. Therefore, there is no natural barrier to communication between the M layer and the C layer, but it is different from the V layer view layer. Yes, this is a cross-language communication. For the browser, it only understands html, javascript and css. The browser cannot understand things in the language java, but to make the server-side things understand and accept by the browser, we will The response information from the server must be placed on the page.

Therefore, a technology is needed to convert java information into html pages. This is the jsp technology provided in the javaEE specification. jsp is actually a server-side technology rather than a client-side technology, but it looks like More like html technology, the earliest jsp development was to write java code directly into the page. Everyone knows this disadvantage. Later, the javaEE specification provided custom tag technology, using a method similar to html tags to parse java code. , the struts2 framework provides a complete set of custom tag technology, which may not sound like much, but its effect is extraordinary, because the reason why custom tags are called custom is that everyone can define them by themselves. If there is no specification There will inevitably be confusion, and a complete set of custom tags is a systematic project. A complete set of custom tags is equivalent to us defining a new development language ourselves. When programmers hear this, they will definitely understand how to develop a set of custom tags. The workload and development difficulty of complete custom tags are unimaginable, and custom tags are closely connected to the control layer, which adds another dimension to the difficulty. Therefore, the custom tags provided by struts2 bring great benefits to business development. It will be a qualitative leap.

There are two important technologies in Servlet: listeners and filters. There are relatively few scenarios where listeners are used in web development. They are only used in some very special situations. They can be ignored in most web development. For its use, the most commonly used listener may be the listener for the creation and destruction of ServletContext. ServletContext is the global object of the entire web application. It is bound to the life cycle of the Web application. Therefore, use this listener to monitor the Web application. Initialize and destroy the global information of the application, such as the initialization operation of the spring container. What is more interesting is the filter. There is an interceptor in struts2. They have the same function and are used to intercept requests. Because interceptors are a unique function of struts2, using interceptors in struts2 is naturally more convenient than using filters. In fact, the technology used by the interceptor is more advanced than the filter. Because the interceptor uses reflection technology, the interceptor intercepts a larger area, has a stronger ability to control requests, and the tasks it can complete will be more colorful.

The first time I came into contact with struts2 was in the gxpt system. Someone told me that one of the purposes of struts design is to block the operation of request and response objects in the control layer, because these two sons of the http protocol will cause The ideas in web development are confusing, but in actual development, I often use these two objects unconsciously. There are too many reflection mechanisms used in struts2, especially the use of annotations for configuration. The execution efficiency of reflection in Java is very low. Direct use of servlets will definitely improve the execution efficiency of web applications. In fact, this was difficult to do because I couldn't flexibly use spring technology in servlet at that time.

Spring technology can be said to be the most important technology in Java enterprise development, but it is really troublesome to really understand the role and significance of spring. Many people’s understanding of spring actually stays at the use stage ( For example: declarative transactions are easy to use, etc.), today’s spring technology ecological environment is spectacular, spring is all-encompassing, and its content is no less than its original Java language, and spring is so big The frames are all built on IOC and AOP technologies. Only by deeply understanding these two technologies can we understand why the Spring frame can hold so many things.

The first is ioc. The first explanation of ioc technology is called inversion of control. Another explanation is dependency injection. These two names are difficult to understand literally, but when you understand its principles, you will Discover how accurately they are described. The essence of IOC technology is the technology of building objects. In other words, it is the technology of instantiating a class into an object. In Java, instantiating a class is done through the new keyword. Every time a new class is created, a new instance object will be generated. Doing this It seems very wasteful, and sometimes this kind of waste is quite dangerous, because when developing programs, we often only need a certain class to produce only one instance object. At this time, we have to use the singleton mode. In addition, in the design mode, we can also pass The factory method generates objects. People who have used spring will know it when they see the above text. The definition of beans in spring corresponds to the above content one-to-one. The scope attribute single generates singleton objects, and prototype generates new objects. Beans can also be passed through The factory method generates objects. It can be said that spring's beans are tools for manufacturing objects. In object-oriented programming, an object is equivalent to displaying an entity in life.

For example, we have an object that is used to complete hunting operations. Then the hunting object contains two auxiliary objects: people and guns. Only people and guns The gun is given the object of hunting, so that the hunting object can complete the hunting operation. However, building an object of a person and a gun is not as simple as it seems. Here is a gun as an example. To create a gun we need metal, machine tools, and Bullets, and machine tools and bullets are two new objects. These objects are nested one by one and related to each other. Just imagine how complicated it is if we construct a gun object in java code. If what we want to construct is not simple The gun object is a more complex aircraft carrier, so the cost of constructing this object is unimaginable. How to eliminate the nesting and interdependence of such objects?

Spring provides a way. This way is that spring provides a container. We define the dependencies of each object in the xml file, and the container completes the construction of the object. When we need to use a certain object in our java code It can be obtained from the container when it is instanced, and then the construction operation of the object is taken over by the spring container, so it is called inversion of control. The meaning of inversion of control is that the function of building objects in the Java program is taken over by the container. Dependency injection means that when a program wants to use an object, the container will inject it into the program. This is called dependency injection.

In java development, we want to use the functions provided by a certain class. There are two ways. One is to construct a new class and the new class inherits the class. The other way is to define a certain class in In the new class, an association relationship is established between the two classes. Spring's ioc container implements this association relationship (remember, it is not an inheritance relationship). So what are the ways to assign a certain class to the new class? Already? Generally there are only two types: one is through the constructor, and the other is through the setXXX method. These are the two standard injection methods used by the spring container.

Whether it is the inheritance method mentioned above or the association method, they are actually development methods to enhance the capabilities of the target object. There is a proxy mode in the design pattern. The proxy mode combines the inheritance mode and the association mode. The agent mode is a synthesis of the inheritance mode and the association mode. However, the function of this synthesis is not to solve the problem of object injection, but to find a nanny or secretary for the specific operation object. This is the same as the No. 2 boss in the novel. This No. 2 leader represents the specific instance object to the outside world. The entrance and exit of the instance object are through this No. 2 leader, because the specific instance object is the No. 1 leader, and the No. 1 leader has to do big things, so some transactional, Repetitive tasks such as making tea and arranging cars do not need to be troubled by the No. 1 head, but are handled by the No. 2 head. This is the idea of ​​AOP. AOP solves the transactional aspects of program development and has nothing to do with the core business. problems, but these problems are necessary for the realization of business scenarios. In actual development, aop is also a way to save code.

The essence of Spring’s core technology is a communication mechanism. Spring always tries its best to keep the information flowing between both parties, while reducing the communication costs of both parties. A person who is good at communication in a real organization It must be the leader of the company. A leader who is good at communication can mobilize the enthusiasm of various resources. A leader who is good at communication will be open to all rivers and let all kinds of people follow him. So today's spring is a big box, everything can be done. It can be installed inside.

Spring is very similar to a bank. It cannot directly create material wealth, but all resources must be circulated through it. It can control the direction of economic development. Returning to the world of programs, the role of spring is to be Billed as decoupling between programs, spring can reduce the coupling between different modules. The reason is that in program development, the communication of information between different modules is completed through object transfer, and whether the object can be transferred smoothly must be reasonable. By building objects well and managing the construction method of objects, you can manage object transfer well. This is the benefit that spring brings to system architecture design.

Related learning recommendations: java basic tutorial

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
Previous article:How to use java foreachNext article:How to use java foreach