2018 Week 16 Wrap-up

Let's talk about the core components of Spring. Spring provides the most basic functional IOC container. In order to realize the IOC container, it is implemented through three components: beans, core, and context. It not only implements the IOC container through a simple factory pattern, but also implements a well-designed interface and extension mechanism. , taking advantage of its advantages of centralized management of object resources and dependencies, and derived related functions such as AOP, transaction management, Spring MVC, etc., even the popular Spring Boot and Spring Could are based on this.

Let 's talk about the factory BeanFactory in Spring, which is the top-level interface of the IOC container. FactoryBean is the SPI interface of the Spring factory bean. It generates bean instances through getObject, which is a typical factory method pattern; ObjectFactory is an object factory, which returns a new instance object through the getObject method. Both FactoryBean and ObjectFactory are used to obtain beans, but the methods and places used are different. After the FactoryBean is configured, Spring calls the getObject() method to obtain the Bean. After the ObjectFactory is configured, the ObjectFactory instance can be obtained in the Bean, which requires us to manually to call getObject() to get the Bean.

Let's talk about the Java SPI mechanism SPI (Service Provider Interface) is a specification defined by Java for service interfaces between different modules. The service provider implements it according to the SPI regulations, which allows the service caller to dynamically replace the service provider module without modifying the code. Thereby reducing the coupling between the service caller and the provider module. There is a similar SPI mechanism between different modules of Spring. Dubbo is also extended on the basis of the SPI mechanism, and realizes more functions on the premise of ensuring that the service caller and the provider are coupled as little as possible.

The method of bean in Spring to obtain IOC container service Now J2EE projects generally use the Spring IOC container, but in many cases, the application needs to obtain some information of the IOC container object itself to achieve some specific functions. Generally, there are two methods. One is The Bean object corresponding to the IOC container is injected directly through the @Autowired annotation (the IOC container itself is also a Bean object). Another method is to implement the corresponding Aware interface, such as BeanFactoryAware, BeanNameAware, ApplicationContextAware, ResourceLoaderAware, ServletContextAware and so on. Beans that implement these Aware interfaces can obtain corresponding resources after being instantiated. For example, after a bean that implements BeanFactoryAware is instantiated, the Spring container will inject an instance of BeanFactory, and a bean that implements ApplicationContextAware, after the bean is instantiated, Instances of ApplicationContext that will be injected, etc. Especially in the static method of the tool class implemented by yourself, this way of implementing the interface is generally adopted.

The extension points in Spring revolve around the IOC container and its bean instantiation process, which are generally divided into three categories of extensions, the BeanFactoryPostProcess interface and its sub-interfaces, which are called before the IOC container instantiates all beans to allow the application to obtain the IOC container bean or bean definition by calling The registry object does some custom operations; the BeanPostProcess interface and its sub-interfaces allow the application implementing the interface to obtain the instantiated bean object to implement some custom functions after the bean instantiation call; the FactoryBean interface allows its implementation class to be customized Bean instantiation process.

Dependency Inversion Principle DIP & Inversion of Control IOC & Dependency Injection DI An understanding of DIP principle, Inversion of Control IOC and dependency injection DI, in order to increase the maintainability of the software, the Dependency Inversion DIP principle should be applied when designing layers. It adopts the inversion of control IOC, and the specific implementation is through dependency injection DI (the instance object it depends on is passed in when the object is instantiated, rather than the temporary creation of the dependent object during the instantiation of the object), which can be used in general projects. Threaded IOC container, such as Spring IOC.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643769&siteId=291194637