Tutorial Series
- Spring Framework Introduction
- Spring Framework module
- Spring development environment to build (Eclipse)
- Create a simple Spring application
- Spring Inversion of Control container (Inversion of Control - IOC)
- Appreciated dependency injection (DI - Dependency Injection)
- Bean XML configuration (1) - through XML configuration loaded Bean
- Bean XML configuration (2) - Bean scope and lifecycle callback method configuration
- Bean XML configuration (3) - configuration dependency injection
- Bean XML configuration (4) - Automatic Assembly
- Bean annotation (Annotation) Configuration (1) - Load Bean via annotations
- Bean annotation (Annotation) Configuration (2) - Bean scope and lifecycle callback method configuration
- Bean Annotations (Annotation) configuration (3) - configuration dependency injection
- Bean Java Configuration
- Spring Aspect Oriented Programming (AOP)
- Spring event (1) - built-in event
- Spring event (2) - Custom Event
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
ApplicationContext
Interface-based BeanFactory
interface than the BeanFactory
more powerful interface is recommended ApplicationContext
. There will be described in detail later.
Bean's configuration
Bean's configuration information includes:
- How to create a bean
- bean life cycle callback method configuration
- 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 -
@Service
or@Component
annotation 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
,@ComponentScan
and@Bean
Follow-up will be described in detail.