Spring: 1. Conceptual understanding

xiaoxiao2021-03-05  22

First, Spring's IOC (Inversion of Control). This is a part of Spring. IOC was also translated into "control reversal", nor did you know who translated this else, it feels very deep. In fact, the principle is very simple, in a popular words: just use XML to define the generated object. IOC is actually a design pattern, and Spring just implements this design mode.

What is this design pattern? It is gradually formed in practice.

The first phase: Write the Java program with ordinary non-mode. General initiators must pass this stage. The second stage: frequently starting using the interface, at this time, the interface is generally accompanied by the use of factory models. The third phase: use IOC mode. Factory model is not good enough: (1) Because the generated code of the class is written in the program, if you have to change a subclass, you must modify the factory method. (2) An interface often means a build factory, there will be many factories. You can see IOC mode as a sublimation of factory models, you can think of IOC as a big factory, but the object to be generated in this big factory is defined in the XML file, then use Java's "reflection" Programming, generates the corresponding object based on the class name given in XML. From the perspective of implementation, IOC generates code to generate a code in the factory method. It is changed to the XML file to define, which is to generate these two of the factories and objects. The purpose is to improve flexibility and Maintainability.

The most basic Java technology in IOC is "reflection" programming. Reflecting is a noun, and it is popular that the reflection is based on the type name (string) given. This programming method can make the object when generated which object to be generated. I also used a reflection in a recent project, which is given a .properties text file, written some all class names (package name class name), then generate them according to these all class names Object. The application of reflection is wide, like hibernate, String is the most basic technical means with "reflection".

In the past, the reflective programming method was more than 10 times relative to normal objects, which may also be the reason why the reflection technology has no ordinary application. However, after SUN improvement optimization, the reflection mode generates objects and usual object generation methods, and the speed has been different (but still more than double the gap).

So understand the IOC, you must first understand the factory model and reflective programming, otherwise it is impossible to understand the cause and the principle of the predominant it. As long as you understand this, you can fully implement an IOC framework in the program, just this also involves other knowledge of XML resolution, slightly troublesome.

What is the biggest benefit of IOC? Since the object is generated in the XML definition, we need to change a subclass will become very simple (generally such objects are realized in some kind of interface), as long as the XML can be, so we You can even realize the thermal plug of objects (a bit like a USB interface and SCIS hard drive).

What is the biggest shortcoming IOC? (1) The steps to generate an object become complex (in fact, it is still very simple), and people who don't get used to this way will feel some awkward and not intuitive. (2) Object generation is due to the use of reflection programming, some loss in efficiency. However, relative to IOC improved maintenance and flexibility, this loss is insignificant, unless the product is particularly high. (3) Missing support for IDE reconstruction operations. If you want to change your name in Eclipse, you still need to change it in the XML file, which seems to be the shortcoming of all XML. In general, IOC is still very simple regardless of principles and implementation. Some people think that IOC has no practical role, this statement is understandable, because if you rarely use interfaces in programming, or rarely use the factory model, then you don't use IOC's strong needs, you will not experience Go to IOC valuable. Some people also say that they should eliminate plant models, single example mode, but they are unknown, people clouds. But if you see IOC mode and use Spring, then factory models and single case models can be basically not available. But is it disappeared? No! Spring's IOC implementation itself is a big factory, which also contains a single case object generation. As long as one setting is used, it can generate a single instance of a single instance by normal ways, very simple.

Summary: (1) The principle of IOC is simple, the role is also very poor, don't see it very embarrassed. (2) To understand the IOC, first understand the concepts of "factory, interface, reflection".

Second, Spring MVC

If you are already familiar with Struts, you don't have to make MVC as a key learning content. Basically I think Spring MVC is a chicken rib, its technology is very advanced, but there is no Struts in ease of use. And Struts has so many years of basis, and Spring is difficult to replace Struts. This is the first outstanding excellent, and a project manager chooses a framework that cannot be simply considered by its technical consideration, and the development efficiency, staff configuration, etc. are considered. But as research-based learning, Spring's MVC part is quite valuable.

Third, the template of the database layer Spring is mainly provided to provide some database templates (templates are also a Java design mode), allowing the code of the data part to be more concise, those try ... catch can not see. This is indeed a good stuff.

Four, AOP

AOP is also known as aspects of programming, its implementation is still reflected: uniform processing by monitoring a certain type of method name. For example: monitoring method names at the beginning of the "Insert" string, perform some processing (database transaction, etc.) before and after this method. But here I have a question? Not necessarily all the methods starting with INSERT are database operations, which is the method of an insert starts is not a database operation, you have a data transaction, such an error is to prevent? ? ? I know that I don't know much about this, or I only know a probably.

I have seen a programmer issued such a feeling: "The frame is one after another, and I have to learn, and it is necessary? Such a layer of formation is not as direct, it is not as direct, efficient." I think this kind of confusion is there? But if you have elapsed, you will find that the maintenance project is more difficult than the development project. It is often written directly with JSP, and the level is unclear, and it is often finally a non-modified software, a mess, moves, and moves. But the software is not like a TV, it will not be changed, the software is a change, the user's needs will change at any time, then you will experience the advantage of the layering and use of the framework, they do for you. There are many works in the software, you can only pay attention to the business and reduce the amount of code. The only shortcoming is that there is a cost of learning, and the frame configuration is also more troublesome. Learning framework, I think it should be: First step, understand some of the key concepts in this framework, what is the specific meaning of it. In the second step, you understand where the essence of this framework is, it can work for development, it is best to have a certain understanding of it. In the third step, use this framework to write a few examples, actually understand. I am still just about two steps. I will look at the Spring document and write a few examples with Spring.

In addition, very much appreciate the view of the author Xia Zhen of the

转载请注明原文地址:https://www.9cbs.com/read-33185.html

New Post(0)