IOC [Spring] of acquaintance


First, the problem


(1) What is the IOC?

Inversion of control (Inversion of Control, IOC), also known as "DI" (Dependency Lookup).

  1. By inversion of control, object creation and transfer referred to IOC container.
  2. There are object references, you can dependency injection.

The so-called IOCcontainer refers to Springthe Beanplant inside Mapthe storage structure (the stored Beaninstance).


(2) Why IOC?

It can be used to reduce the degree of coupling between the computer code.

Relationship better management of the object itself, between objects and objects.

advantage:

  1. Management class is created, destroyed
  2. Dependencies between classes and categories (for example: circular dependencies, etc.)
  3. To avoid the object creation is not standardized, inconsistent
  4. Decoupling
  5. Allowing developers to pay more attention to business development

(3) how to create IOC?

Spring IOC container loading Beanmodes: XMLconfiguration mode and annotations


1. XMLconfiguration

By beanLabel

public class Application {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Car car = (Car) applicationContext.getBean("car");
        
        Car car2 = (Car) applicationContext.getBean(Car.class);

     }
}

2. Notes manner

  1. On the class corresponds to annotate required.

@Component @Controller @Service Repositoryand many more

  1. Scanning dependent configuration package
    context:component-scan

Under scanning corresponding annotation packet, the class loader into the annotation IOC container

Annotation mode, the sample code:

@ComponentScan(basePackages = "com.donaldy")
public class Application {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);

        Car car = (Car) applicationContext.getBean("car");

     }
}

How (4) IOC container is initialized Bean instance?

Figure:
Here Insert Picture Description



Second, some of the concepts


First understand the meaning of the concept and its name represents.

(1)BeanFactory

beanFactory: bean plant

Bean production


(2)ApplicationContext

applicationContext: Application Context

Global Overview


(2)BeanDefinition

definition: the definition

That is, each beanmessage has its own definition: Property, class name, type, etc.

Interdependence between the object reaction

Published 404 original articles · won praise 270 · views 420 000 +

Guess you like

Origin blog.csdn.net/fanfan4569/article/details/101910307