This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

The IOC container

  • Ioc containers are called containers because they contain managed beans
  • All IOC containers must be implementedBeanFactoryinterface

The BeanFactory source

package org.springframework.beans.factory;  
import org.springframework.beans.BeansException;  
public interface BeanFactory {  
    String FACTORY_BEAN_PREFIX = "&";
    // various methods of getBean
    Object getBean(String name) throws BeansException;  
    <T> T getBean(String name, Class<T> requiredType) throws BeansException;  
    <T> T getBean(Class<T> requiredType) throws BeansException;  
    Object getBean(String name, Object... args) throws BeansException; 
    // Whether to include beans
    boolean containsBean(String name);  
    // Whether the Bean is a singleton
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;  
    // Whether the Bean is a prototype
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException; 
    // Whether the types match
    boolean isTypeMatch(String name, Class
        targetType) throws NoSuchBeanDefinitionException; 
    // Get the Bean typeClass<? > getType(String name)throws NoSuchBeanDefinitionException;  
    // Get the Bean alias
    String[] getAliases(String name);  
}
Copy the code
  • We can see various types of GetBeans, which in the IOC container can be retrieved by type or nameBean
  • By default, beans are singletons, so getBean returns the same object, which is used hereisSingleton

Springboot assembly and retrieval beans

  • Create the entity class first (POjo-stu)

  • The configuration file

  • Among them@BeanIt is in the configuration file that IOC generates the IOC container assembly Bean based on this annotation
  • @BeanThis means assembling the return value of the initStu method (the STU of the POJO) into the IOC container

  • After completing the above operations we can use itAnnotationConfigApplicationContext
  • canApplicationContext t = new AnnotationConfigAppl cationContext(AppConfig.class);
  • stu s = t.getBean(stu.class)To get the bean to stU

The child eng has assembled a Bean

  • During development, we will not use Bean annotations to inject each one into Spring’s IOC container
  • So Spring is assembled by scanning, using @Component and @ComponentScan annotations
  • The @Component can indicate which classes can be scanned into Spring’s IOC container
  • @ComponentScan can directly indicate what strategy is used to scan assembly beans

ComponentScan

  • Add above the STU of the POJO above@ComponentTo indicate that the class will be scanned and assembled by the Sring IOC container
  • Add above ApplicationConfig@ComponentScanAnnotation to scan only the current ApplicationConfig package
  • In fact, we can customize the location of the scan, to see the source of ComponentScan annotations

  • AliasFor is the package that can define a scan

  • BasePackxxx can define the packets to scan

  • NameGenerator is the Bean name generator

  • ScopeResolver Scope proxy mode

  • IncludeFilters scan when filter conditions are met

  • ExcludeFilters scan when filter conditions are not met

  • BasePackages is used to scan packet names

  • Only those with xxxFileter are defined as benAs that meet the filter criteria