Spring framework IoC underlying core principles

5) IoC underlying core principles

5.1) IoC core interface

Insert picture description here

5.2) Component scanner

  • During the development process, necessary beans need to be loaded according to requirements, and specified beans are excluded

Insert picture description here

5.3) Set component scan loading filter

  • Name: @ComponentScan

  • Type: Class annotation

  • Location: Above the class definition

  • Role: set spring configuration loading class scanning rules

  • example:

    @ComponentScan(
        value="com.itheima",	           //设置基础扫描路径
        excludeFilters =                          //设置过滤规则,当前为排除过滤
    	@ComponentScan.Filter(            //设置过滤器
    	    type= FilterType.ANNOTATION,  //设置过滤方式为按照注解进行过滤
    	    classes=Repository.class)     //设置具体的过滤项,过滤所有@Repository修饰的bean
        )
    

​ includeFilters: set the inclusion filter

​ excludeFilters: set exclusion filters

​ type: Set the filter type

5.4) Custom component filter

  • Name: TypeFilter

  • Type: Interface

  • Role: custom type filter

  • example:

    public class MyTypeFilter implements TypeFilter {
          
          
        public boolean match(MetadataReader mr, MetadataReaderFactory mrf) throws IOException {
          
          
            ClassMetadata cm = metadataReader.getClassMetadata();
            tring className = cm.getClassName();
            if(className.equals("com.itheima.dao.impl.BookDaoImpl")){
          
          
                return false;
            }
            return false;
        }
    }
    

5.5) Custom importer

  • The bean can enter the spring container only through configuration, and be loaded and controlled by spring
  • The way to configure the bean is as follows:
    • Use tag configuration in XML file
    • Use @Component and derivative annotation configuration
  • In the process of enterprise development, usually a large number of beans need to be configured, and a way to configure a large number of beans quickly and efficiently is required

ImportSelector

  • Name: ImportSelector

  • Type: Interface

  • Role: custom bean importer

  • example:

    public class MyImportSelector implements ImportSelector {
          
          
        public String[] selectImports(AnnotationMetadata icm) {
          
          
            return new String[]{
          
          "com.itheima.dao.impl.AccountDaoImpl"};
        }
    }
    
    @Configuration
    @ComponentScan("com.itheima")
    @Import(MyImportSelector.class)
    public class SpringConfig {
          
          
    }
    

5.6) Custom registrar

  • Name: ImportBeanDefinitionRegistrar

  • Type: Interface

  • Role: custom bean definition registrar

  • example:

    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
          
          
        public void registerBeanDefinitions(AnnotationMetadata icm, BeanDefinitionRegistry r) {
          
          
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(r, false);
            TypeFilter tf = new TypeFilter() {
          
          
                public boolean match(MetadataReader mr, MetadataReaderFactory mrf) throws IOException {
          
          
                    return true;
                }
            };
            scanner.addIncludeFilter(tf);
            //scanner.addExcludeFilter(tf);
            scanner.scan("com.itheima");
        }
    }
    

5.7) Analysis of bean initialization process

Insert picture description here

5.8) Analysis of bean initialization process

  • BeanFactoryPostProcessor
    • Role: Defines the actions performed before the bean object is created after the bean factory object is created, and is used to perform post-creation business processing on the factory
    • Run time: The current operation is used to process the factory and only run once
  • BeanPostProcessor
    • Function: Defines the unified actions performed before and after the initialization of all beans, which are used to perform pre-creation business processing and post-creation business processing on the bean
    • Runtime: The current operation is accompanied by the creation process of each bean, and the operation is run every time a bean is created
  • InitializingBean
    • Role: Defines the actions to be performed before the initialization of each bean, which is a non-uniform action and is used to perform business processing on the bean before creation
    • Runtime: The current operation is accompanied by the creation process of any bean to ensure its personalized business processing
  • Note: The above operations need to be loaded by the spring container to run

5.9) Analysis of bean initialization process

Insert picture description here

5.10) tedious bean initialization process processing

  • FactoryBean
    • Encapsulate the initialization process of a single bean to achieve the purpose of simplifying configuration

The difference between FactoryBean and BeanFactory

  • FactoryBean: Encapsulate the creation process of a single bean
  • BeanFactory: the top-level interface of the Spring container, which defines bean-related acquisition operations

Guess you like

Origin blog.csdn.net/xghchina/article/details/113923977