Spring Inversion of Control container (Inversion of Control - IOC)


Tutorial Series


IoC (Inversion of Control) control on behalf of inversion, the frame is essentially a Spring IoC container.

What is Inversion of Control?

Java program, Java class is the basic unit of code organization. When a class to use another class, the natural approach is to create objects of another class, then call the method of the object. To do so in the smaller size of the program no problem, but too tight coupling between these classes is large-scale program will lead to subsequent development and maintenance difficulties. For example, to replace one of these categories, we have to change all the code involved in this class.

Inversion of control (Inversion of Control / IoC) and dependency injection (Dependency Injection / DI) is to solve this problem. The above-mentioned categories which create another instance of the class will result in tight coupling, put the Spring Framework to create instances of the class acts out of the class into the Spring framework to do it, create a relationship between classes is not.

Another instance of the class have been created on which it depends is controlled by the class, now put into action to create a Spring framework, and the framework created by the instances of all classes of control, this is the "inversion of control."

Framework creates a class dependent objects, then these objects are passed (injected) class, which is the "dependency injection."

So you can see, Spring Framework inversion of control (Inversion of Control / IoC) and dependency injection (Dependency Injection / DI) is actually talking about the same thing, but a different angle.

The main task of IoC container to perform:

  • Creation bean instance
  • The profile fitting bean
  • Set initialization parameters for the bean
  • Bean life cycle management

What is Bean?

Java Bean is the basic unit of code reuse, Java class is to comply with certain conventions.

In Spring, Java Bean is an ordinary class, is instantiated by the Spring IoC container, assembly, management.

IoC container according to the configuration XML file, Java annotations or Java config file to create, assemble, management bean (plain Java classes), spring application only statement about the required class (often a parent, in order to achieve loose coupling ) bean can use these functions. as the picture shows:

IoC container There are two types:

  • BeanFactory
  • ApplicationContext

ApplicationContextInterface-based BeanFactoryinterface than the BeanFactorymore powerful interface is recommended ApplicationContext. There will be described in detail later.

Bean's configuration

Bean's configuration information includes:

  1. How to create a bean
  2. bean life cycle callback method configuration
  3. Dependence of the bean

Using this information Spring container assembly Bean.

bean configuration There are 3 ways:

  • XML configuration - using XML files to configure bean
  • Notes configuration - @Serviceor @Componentannotation bean configuration
  • Java configuration - from the beginning of Spring 3.0, you can configure the program to use java bean, the main java annotation is used to configure: @Configuration, @ComponentScanand@Bean

Follow-up will be described in detail.

Guess you like

Origin www.cnblogs.com/jinbuqi/p/10959100.html