Spring: 1. Concepts

Source: Internet
Author: User

I. Spring IoC (inversion of control ).
This is a unique part of spring. IOC has been translated into "control inversion", and I don't know who translated the words so awkwardly. In fact, the principle is very simple. In plain words, the generated object is defined using XML. IOC is actually a design pattern. Spring only implements this design pattern.

How does this design model come from? Is gradually formed in practice.

Stage 1: Write Java programs in normal mode. Generally, beginners must go through this stage.
Stage 2: The interface is frequently used. In this case, the interface is usually used in the factory mode.
Stage 3: Use the IOC mode. The factory mode is not good enough: (1) because the generated code of the class is written into the program, if you want to change a subclass, You need to modify the factory method. (2) An interface often means that a production factory will have many more factory classes.
The IOC mode can be seen as the sublimation of the factory mode, and IOC can be seen as a large factory, but the objects to be generated in this large factory are defined in the XML file, then, use Java's "reflection" programming to generate the corresponding object based on the class name given in XML. From the implementation point of view, IOC is to generate code for the previously written object in the factory method, which is defined by the XML file, that is, to separate the factory and object generation, the purpose is to improve flexibility and maintainability.

The most basic Java Technology in IOC is reflection programming. Reflection is a natural term. In general, reflection is used to generate an object based on the given class name (string. This programming method allows an object to decide which object to generate when it is generated. I also used reflection in a recent project. At that time, I provided one. in the properties text file, full class names (package name + class name) are written, and their objects are generated in the program according to these full class names. Reflection is widely used. For example, Hibernate and string use reflection as the most basic technical means.

In the past, the reflection programming method was 10 times slower than the normal object generation method, which may be why the reflection technology was not widely used at the time. However, after Sun's optimization, the reflection method generates the object and the normal object generation method, and the speed is not much different (but there is still a gap more than doubled ).

Therefore, to understand IOC, you must first understand the factory model and reflection programming. Otherwise, you cannot fully understand the cause and effect and implementation principles of IOC. As long as you understand this, you can also implement an IOC framework in your program. This is not only related to XML parsing and other knowledge, it is a little troublesome.

What are the biggest benefits of IOC? Because the object generation is defined in XML, when we need to change an implementation subclass, it will become very simple (generally, such an object is actually an interface ), you only need to modify the XML, so that we can even implement the Hot Plug-in of the object (a bit like the USB interface and scis hard disk ).

What are the biggest disadvantages of IOC? (1) The process of generating an object becomes complicated (in fact, the operation is quite simple). For those who are not used to this method, they will feel awkward and inintuitive. (2) object generation is inefficient because reflection programming is used. However, this loss is negligible compared with IOC's increased maintainability and flexibility, unless the generation of an object requires a particularly high efficiency. (3) There is a lack of support for IDE refactoring operations. If you want to rename the class in eclipse, you still need to manually change it in the XML file, this seems to be the shortcoming of all XML methods.

In general, the principle and implementation of IOC are quite simple. Some people once think that IOC has no practical effect. This is understandable because if you seldom use interfaces or use factory models in programming, therefore, you do not have a strong need to use IOC at all, and you will not understand the value of IOC. Some people also say they want to eliminate the factory and Singleton models, but they are all vague and clear. However, if you see the IOC mode and use spring, the factory mode and Singleton mode are basically useless. But does it disappear? No! Spring's IOC implementation is itself a big factory, which also includes the single-instance object generation method. As long as one setting is used, the object generation mode can be changed from the common mode to the single instance mode, it is very simple.

Summary:
(1) the IOC principle is very simple, and its role is also very targeted. Do not look at it very well.
(2) To understand IOC, first understand the concepts of "factory, interface, reflection.

Ii. Spring MVC

If you are familiar with Struts, you do not need to take MVC as the key learning content. Basically, I think spring MVC is a weakness. It is technically advanced, but it is not as easy to use as struts. Moreover, with the foundation of struts for so many years, it is difficult for spring to replace struts. This is the primary advantage. A project manager chooses a framework and cannot simply consider its technology, development efficiency, and staffing. But as a research study, the MVC part of spring is quite valuable.

Iii. Database-layer templates
Spring mainly provides some database templates (the template is also a Java design pattern), so that the data part of the code is more concise, and those try... catch can be lost. This is indeed a good thing.

Iv. AOP

AOP is also called Aspect-Oriented Programming. Its implementation principle is reflection: it implements unified processing by monitoring the method name of a certain type. For example, monitor the name of a method starting with an "insert" string, and perform some processing (database transactions, etc.) before and after this method is executed ). But here I have a question? Not all methods starting with "insert" are database operations. When a method starting with "insert" is not a database operation, you have performed data transaction operations on it, how to prevent such errors ??? I do not know much about this, or I only know a rough idea.

I have seen a programmer send this feeling: "The framework is one by one, and I can't finish learning it. Is it necessary? In this way, adding the framework layer by layer is not as efficient as writing JSP directly ". I think many people have this confusion, right? However, if you go through more and more projects, you will find that the maintenance project is more difficult and costly than the development project. The kind of development that uses JSP to write directly and with unclear layers often gets a software that cannot be modified at last. However, when the software is not like a TV set, it will not be changed. The software is a changing thing and the user's needs will change at any time. At this time, you will experience the advantages of layering and using the framework, they do a lot of business-independent work for your software. You can focus only on the business and reduce the amount of code. The only drawback is that there is a learning cost, and framework configuration is also troublesome.

I think the learning framework should be: the first step is to understand some key concepts in the Framework, and what is its specific meaning. Step 2: understand the essence of this framework and what role it can play in development. It is best to have a certain understanding of its principles. Step 3: use this framework to write several examples. I have just completed the first two steps. I will take a look at the spring documents and use spring to write several examples. Then I will release them together.

In addition, Xia Xin, author of the <spring Development Guide>, expressed his appreciation for his point of view and published his own experiences as a document. However, the strength of a person is too weak. It is better to form an organization. For a new technology, one or two people have an outline. You have divided it into more chapters, and then two or three people have a general set. I personally feel that due to the English language, the introduction of new technologies into China is still too slow, at least more than a year later than that in other countries. It makes sense to set up an open-source document organization.

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.