The origin of spring framework and the basic concept of IOC

Source: Internet
Author: User

I. Overview of the Spring framework

The lightweight development concept of the spring framework, based on Pojo (Plain old Java object, simple Java objects), is to start from the ground up, based on the most basic pojo. To enable these basic pojo to build robust and powerful applications, the spring framework, like that of the Earth's atmosphere, provides a variety of services for building Applications Pojo, creating an environment that adapts to the lightweight development of Pojo.

Broadly speaking, no matter how many versions of the spring framework since its release to now (from 1.X to 2.0 to 2.5), the essence is always the same, to provide a variety of services to help us simplify the development of Pojo-based Java applications. The spring Framework provides a variety of services for Pojo, which together form the spring tree of Life

The various service implementations that make up the entire spring framework are divided into separate, interdependent modules. As we have seen in the diagram, these modules form the branches and the stem of the spring tree of Life, which is the core skeleton of the spring framework. Grasp the skeleton, also grabbed the spring framework of the learning line.

The entire spring framework is built on top of the core kernel module, which is the foundation of the entire framework. In this module, Spring provides us with an IOC container (IOC Container) implementation that helps us manage dependencies between objects in a dependency injection manner. The introduction of the spring IOC container will be our first stop on this spring tour. In addition, core core modules also include various tool classes used within the framework (we can also use outside the framework if we like), such as spring's basic IO Tools class, which we will also introduce in the appropriate places.

To simplify the use of various Java EE services such as Jndi JMS and JavaMail, the srping framework provides us with inheritance services for these Java EE services. With the help of spring, these Java EE services are now becoming less cumbersome. Because of the more relevant Java EE Services, we will choose the appropriate introduction spring framework to give them support. With the development of aerospace technology, we can now send ships from Earth to visit other planets, using the spring framework to build Pojo-based applications If you also need to remotely access or expose some services, spring's remoting framework will help it accomplish this mission. Spring's remoting framework and spring's integration into other Java EE services are described in separate chapters.

The above is a general introduction to the entire Spring framework. Before starting a pleasant spring trip, I would like to take you around the spring garden, so that everyone will find the spring trip that is about to begin is more than expected.

Note: Do not consider spring as an IOC container, and do not just hook up spring with AOP. Spring offers far more than these things. Spring is not just a lightweight framework for simplifying Java EE Development, it should be a development framework that simplifies any Java application. If you want, you can even apply the spring framework on three platforms in Java (J2SE, Java EE, J2ME). Even if the current spring framework does not support the application development of the corresponding platform or the corresponding scenario, it is also possible for the new "Spring" to function in the corresponding scenario as long as you have mastered the concepts and methods of spring.

Second, Spring Grand Garden

  When Java was born as a computer language in the 1995, who could have thought that it had developed into a powerful development platform in just more than 10 years? For the spring framework, history repeats itself and is almost without suspense.

The Spring garden has a towering tree that thrives, mainly because it has a good foundation, and that is the spring framework. On the basis of the spring framework, the Spring family jizya began to flourish, with the emergence of one more notable family member

Iii. Basic concepts of the IOC

  The IOC is a noun that has gradually been brought up by many people with the rise of lightweight containers (Lightweughtcontainer) in recent years, which are all called inversion of control, and Chinese is usually translated as "inversion of controls", It also has an alias called Dependency Injection (Dependency injection). The Hollywood principle "Don T call Us,we would call you" aptly expresses the meaning of "reversal", which is used to describe the most words of the IOC. So why do we need the IOC? What is the specific meaning of the IOC? What's so unique about it? Let's start our IOC tour with these questions for the break.

In fact, the IOC was designed to help us avoid the "big hassle" of the past and provide a much easier and simpler way. Its reversal, on the reverse, allows you to transform from the original hands to the current enjoyment of the service. You want Ah, originally still have to Guemarauton, is what thing all have to take. Now it is with what, let others directly send over to become. So, simply put, the IOC idea is to let others serve you, that is, to let the IOC service provider to serve you!

  Typically, the injected object is directly dependent on the dependent object. However, in the IOC scenario, the two interact with the IOC service Provider, and all injected objects and dependent objects are now managed uniformly by the IOC service Provider. What needs to be injected into the object, directly with the IOC service provider, the latter will be the corresponding dependent objects injected into the object of interest, so as to achieve the IOC service provider for the purpose of being injected into the object. The IOC Service provider is here the most common role of the IOC container. From the point of view of the injected object, the way in which the dependent objects are obtained is reversed, and the control is transferred from the injected object to the IOC Service provider, compared to the previous direct search for the dependent object.

In fact, the IOC is so simple! The original is to need something to get it yourself, now is what you need to let others send over. The image illustrates the difference before and after using the IOC model.

Construct Method Injection

As the name implies, a constructor injection is an injected object that allows an external (usually IOC container) to know which dependent objects it needs by declaring the argument list of the dependent object in its construction method.

Constructor method definition

The Ioc Service Provider examines the constructed method of the injected object, obtains the list of dependent objects it needs, and then injects the corresponding object into it. The same object is not likely to be constructed two times, so the structure of the injected object, and even its entire life cycle, should be managed by the IOC Service provider.

Construction method Injection method is more intuitive, after the object is constructed, that is, to enter the ready state, can be used immediately. It's like you just got into the bar door and the waiter has put your favorite beer on the table. Sit down and enjoy a refreshing and relaxing moment.

Setter Method Injection

For JavaBean objects, the corresponding properties are usually accessed through the setxxx () and GetXXX () methods. These setxxx () methods are collectively referred to as setter methods, and GetXXX () is, of course, called Getter methods. Through the setter method, you can change the corresponding object properties, through Getter method, you can get the state of the corresponding property. Therefore, as long as the current object adds a setter method for the property of its dependent object, the corresponding dependent object can be set to the injected object through the setter method.

  This allows the outside world to inject the dependent object into the Fxnewsprovider object by invoking the Setnewslistener and Setnewpersistener methods.

Setter method Injection, although not like the construction method injection, so that the object construction can be used, but relatively more relaxed, you can build the object after the completion of the injection. It's like you can sit down at a bar and decide what to order. More casual.

Interface Injection

Interface injection is less straightforward than the first two injection methods. Injected object if you want the IOC Service provider to inject its dependent objects, you must implement an interface. This and the interface provide a way to inject the dependent object into it. The IOC Service provider finally uses these interfaces to understand what dependent objects should be injected into the injected object, demonstrating how to inject the dependent object into the Fxnewsprovider using interface injection

  Interface Injection method is the earliest and most used in a project called AVALN, compared to the first two types of dependency injection, interface injection is more rigid and cumbersome. If you need to inject a dependent object, the injected object must declare and implement another interface.

The origin of spring framework and the basic concept of IOC

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.