Spring lets the program generate programs

Source: Internet
Author: User

Spring's main concept: Dependency Injection and control inversion

Dependency Injection (Dependency injection) and control inversion (inversion of controls) are the same concept.

The implication is that when a role (possibly a Java instance, the caller) needs the assistance of another role (another Java instance, the callee), the caller's instance is typically created by callers during the traditional program design process.

In spring, however, the work of creating the callee is no longer done by the caller, so called control inversion, and the work of creating the callee instance is usually done by the spring container, which is then injected into the caller and, therefore, also called dependency injection.

evolution of the Java programming Paradigm:

(1) In primitive society, there was hardly any social division of labor. The person who needs the axe (the caller) can only grind an axe (callee) on its own. The corresponding scenario is: The caller in the Java program creates the callee itself.

(2) Enter the industrial society, the factory appears. The axe is no longer done by ordinary people, but is produced in the factory, where the person needing the axe (the caller) finds the factory, buys the axe, and does not care about the manufacturing process of the axe. A simple factory design pattern that corresponds to a Java program.

(3) To enter the "On demand" society, people who need an axe do not need to find a factory, sitting at home to send a simple command: Need an axe. The axe appeared to him naturally. Corresponds to spring's dependency injection.

In the first case, the caller of the Java instance creates the invoked Java instance, which inevitably requires the called Java class to appear in the caller's code. The loose coupling between the two cannot be achieved.

In the second case, the caller does not have to care about the specific implementation of the callee, only need to find an instance that conforms to a certain standard (interface) to be used. The code invoked at this point is interface-oriented programming, which allows the caller and callee to be decoupled, which is why the factory pattern is heavily used. But the caller needs to locate the factory itself, and the caller is coupled with the particular factory.

In the third case, the caller does not need to locate the factory himself, and the system automatically provides the callee instance when the program runs to the caller. In fact, both the caller and the callee are under Spring's management, and the dependencies between them are provided by spring.

Spring lets the program generate programs

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.