Article Directory
First, the problem
(1) What is the IOC?
Inversion of control (Inversion of Control, IOC), also known as "DI" (Dependency Lookup).
- By inversion of control, object creation and transfer referred to IOC container.
- There are object references, you can dependency injection.
The so-called IOC
container refers to Spring
the Bean
plant inside Map
the storage structure (the stored Bean
instance).
(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:
- Management class is created, destroyed
- Dependencies between classes and categories (for example: circular dependencies, etc.)
- To avoid the object creation is not standardized, inconsistent
- Decoupling
- Allowing developers to pay more attention to business development
(3) how to create IOC?
Spring IOC container loading Bean
modes: XML
configuration mode and annotations
1. XML
configuration
By bean
Label
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
- On the class corresponds to annotate required.
@Component
@Controller
@Service
Repository
and many more
- 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:
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 bean
message has its own definition: Property, class name, type, etc.
Interdependence between the object reaction