DI and IOC, the difference between BeanFactory and ApplicationContext, Spring design container, dependency injection method, bean life, @Autowired, introduction of Spring annotation

1 introduce Spring dependency injection and control rollover

IOC: It transfers the calling rights of objects that are traditionally directly manipulated by program code to the container, and implements the assembly and management of object components through the container ; the realization principle of IoC in Spring is the factory mode and the reflection mechanism ;

2What is the difference between BeanFactory and ApplicationContext?

Insert picture description here

BeanFactory and ApplicationContext are the two core interfaces of Spring, and both can be used as Spring containers. The ApplicationContext is a sub-interface of BeanFactory. BeanFactory : It is the lowest interface in Spring. It contains definitions of various Beans, reads bean configuration documents, manages bean loading and instantiation, controls bean life cycle, and maintains dependencies between beans. As a derivation of BeanFactory, ApplicationContext interface not only provides the functions of BeanFactory, but also provides more complete framework functions: (MessageSource) supports internationalization, etc.;

BeanFactroy uses lazy loading to inject Beans , that is, only when a Bean is used (getBean() is called), the Bean is loaded and instantiated . In this way, we cannot find some existing Spring configuration problems. If a certain attribute of Bean is not injected, after BeanFacotry is loaded, an exception will not be thrown until the getBean method is called for the first time. ApplicationContext , which creates all Beans at once when the container is started . In this way, when the container starts, we can find configuration errors in Spring, which is conducive to checking whether the dependent properties are injected . After the ApplicationContext starts, all single-instance beans are pre-loaded. By pre-loading single-instance beans, you can ensure that when you need them, you don't have to wait because they have already been created.

BeanFactory is usually created programmatically, ApplicationContext can also be created in a declarative way , such as using ContextLoader.

3 How does Spring design the container and explain the relationship between BeanFactory and ApplicationContext?

Spring author Rod Johnson designed two interfaces to represent the container: 1BeanFactory 2ApplicationContext
BeanFactory is a HashMap, Key is BeanName, Value is Bean instance. Usually only provide registration (put), access (get) these two functions. We can call it a " low-level container ". The ApplicationContext can be referred to as an " advanced container ". He inherited multiple interfaces, so it has more functions. For example , the acquisition of resources , support for multiple messages (such as JSP tag support), "application context", represents all the functions of the entire large container. This interface defines a refresh method for refreshing the entire container , that is, reloading/refreshing all beans .
The relationship between BeanFactory and ApplicationContext:
Insert picture description here
ApplicationContext "high-level container", depends on "low-level container" , here is the dependency, not inheritance . He relies on the getBean function of the "low-level container" . The advanced container has more functions: supports different information sources, can access file resources, and supports application events (Observer mode). In Spring, IoC can be implemented only by a low-level container. There are two steps:
1. Load the configuration file, parse it into a BeanDefinition and place it in the Map .
2 When calling getBean, take out the Class object from the Map to which the BeanDefinition belongs to instantiate it. At the same time, if there is a dependency, it will recursively call the getBean method to complete the dependency injection .
As for the high-level container ApplicationContext, it contains the functions of the low-level container. When it executes the refresh template method, it will refresh the bean of the entire container . At the same time, as an advanced container, it contains too many functions .

4What are the different types of dependency injection implementations?

Dependency injection is the most popular IoC implementation method nowadays. Dependency injection is divided into three methods: Interface Injection , Setter Injection and Constructor Injection. Interface injection has been abandoned since Spring 4 due to its poor flexibility and ease of use.
Insert picture description hereThe best solution is to use constructor parameters to implement mandatory dependencies and setter methods to implement optional dependencies.

5 Explain the life cycle of beans in the Spring framework?

Insert picture description here
1 Spring instantiates the bean ;
2 Spring injects the value and bean reference into the corresponding attribute of the bean ; (property injection)
3 If the bean implements the BeanNameAware interface, Spring will pass the bean ID to the setBean-Name() method ;
4 If the bean implements the BeanFactoryAware** interface, **Spring will call the setBeanFactory() method to pass in the BeanFactory container instance;
5 If the bean implements the ApplicationContextAware interface, Spring will call the setApplicationContext() method to set the application context where the bean is located The reference of comes in;

If the bean implements the BeanPostProcessor interface, Spring will call their post-ProcessBeforeInitialization() method;

If the bean implements the InitializingBean interface, Spring will call their after-PropertiesSet() method. Similarly, if the bean uses the initmethod to declare the initialization method, the method will also be called;

If the bean implements the BeanPostProcessor interface, Spring will call their post-ProcessAfterInitialization() method;

At this point, the beans are ready to be used by the application, and they will remain in the application context until the application context is destroyed ;

If the bean implements the DisposableBean interface, Spring will call its destroy() interface method. Similarly, if the bean declares a destroy method using destroy-method, that method will also be called.

Now you have understood how to create and load a Spring container. But an empty container doesn't have much value. Before you put things in, there is nothing in it. In order to benefit from Spring's DI (dependency injection), we must assemble the application object into the Spring container.

6 What is the process of using @Autowired annotations to auto-assemble?

Use the @Autowired annotation to automatically assemble the specified bean. Before using @Autowired annotation, you need to configure it in the Spring configuration file, <context:annotation-config />.

When spring IoC is started, the container automatically loads an AutowiredAnnotationBeanPostProcessor post processor . When the container scans for @Autowied, @Resource or @Inject, it will automatically find the required bean in the IoC container and assemble it to the object's properties. When using @Autowired, first query the corresponding type of bean in the container:

If the query result is exactly one, the bean will be assembled to the data specified by @Autowired;
if the query result is more than one, then @Autowired will search based on the name ;
if the result of the above search is empty, an exception will be thrown. When solving, use required=false.

7 Introduction to Spring annotations?

Take the @Configuration annotation as an example. It is used to mark a class as a bean definition and used by the Spring IOC container. @Bean annotation , it indicates that this method will return an object, which is registered as a bean in the Spring application context .

1 What is the difference between @Component, @Controller, @Repository, @Service?

@Component: This marks the java class as bean . It is a general stereotype for any Spring management component . Spring's component scanning mechanism can now pick it up and pull it into the application environment.

@Controller: This marks a class as a Spring Web MVC controller . Beans marked with it will be automatically imported into the IoC container.

@Service: This annotation is a specialization of component annotations. It does not provide any other behavior for the @Component annotation. You can use @Service instead of @Component in the service layer class because it specifies the intent in a better way .

@Repository: This annotation is a specialization of the @Component annotation with similar uses and functions. It provides additional benefits for DAO . It imports the DAO into the IoC container and makes unchecked exceptions eligible to be converted to Spring DataAccessException.

2 What does the @Required annotation do?

This annotation indicates that the bean properties must be set during configuration. If the bean properties of the @Required annotation are not set, the container will throw a BeanInitializationException.

3 What does the @Autowired annotation do?

@Autowired is injected by type assembly by default. By default, it requires dependent objects to exist (you can set its required attribute to false ).

4 What is the difference between @Autowired and @Resource?

@Autowired is injected by type assembly by default. By default, it requires that the dependent object must exist (you can set its required attribute to false).
@Resource assembles injection according to the name by default . Only when no bean matching the name is found, the injection is assembled according to the type.

5 What does the @Qualifier annotation do?

When you create multiple beans of the same type and want to assemble only one of the beans with attributes , you can use the **@Qualifier annotation and @Autowired to disambiguate by specifying which exact bean should be assembled**.

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/115275327
Recommended