Spring Framework source code: BeanFactory of spring beans

Source: Internet
Author: User

Spring Framework source code: BeanFactory of spring beans
First, let's take a look at the class diagrams of two important containers BeanFactory and ApplicationContext:

Next, let's introduce the classes:

??public interface BeanFactory {String FACTORY_BEAN_PREFIX = "&";Object getBean(String name) throws BeansException;
 
   T getBean(String name, Class
  
    requiredType) throws BeansException;
   
     T getBean(Class
    
      requiredType) throws BeansException;Object getBean(String name, Object... args) throws BeansException;
     
       T getBean(Class
      
        requiredType, Object... args) throws BeansException;boolean containsBean(String name);boolean isSingleton(String name) throws NoSuchBeanDefinitionException;boolean isPrototype(String name) throws NoSuchBeanDefinitionException;boolean isTypeMatch(String name, Class
        targetType) throws NoSuchBeanDefinitionException;Class
        getType(String name) throws NoSuchBeanDefinitionException;String[] getAliases(String name);}
      
     
    
   
  
 

BeanFactory base class, including the following members:

String FACTORY_BEAN_PREFIX = "&"; a constant member that is used to reference A FactoryBean instance and differentiate and program a series of beans created by FactoryBean. For the meaning of the resolution, see C ++ programming.

For example, A FactoryBean A gets A factory rather than A bean instance.

Object getBean (String name) throws BeansException; get bean instance by name

T getBean (String name, Class RequiredType) throws BeansException; a generic method that obtains bean instances by class names and classes.

T getBean (Class RequiredType) throws BeansException; a generic method that obtains bean instances by class type.

Object getBean (String name, Object... args) throws BeansException; create an instance by name and display declared parameters. Note that the declared parameters are used to create a prototype.

T getBean (Class RequiredType, Object... args) throws BeansException;

Boolean containsBean (String name); does the bean Factory contain a bean named name?

Boolean isSingleton (String name) throws NoSuchBeanDefinitionException; whether the bean is a singleton

Boolean isPrototype (String name) throws NoSuchBeanDefinitionException; whether the bean is prototype

Boolean isTypeMatch (String name, Class TargetType) throws NoSuchBeanDefinitionException; whether the class type of the bean named name is targetType

String [] getAliases (String name); obtain all dependencies of all beans. For details, see the spring configuration file.

The next step is HierarchicalBeanFactory, one of its two main sub-interfaces. It is literally a layered Bean Factory. From the provided function interfaces, the channel to the parent container is added based on BeanFactory, but it can only be obtained (Parent and Child containers. For example, ApplicationContext is the parent container, DispatcherServlet is a sub-container that can have multiple sub-containers, and the sub-container can get and pay the container to access its content, otherwise it will not work ):

public interface HierarchicalBeanFactory extends BeanFactory {BeanFactory getParentBeanFactory();boolean containsLocalBean(String name);}

BeanFactory getParentBeanFactory (); get parent container

Boolean containsLocalBean (String name); determines whether the current container contains a bean named name and ignores the bean in the parent container.

The following is another important sub-interface ListableBeanFactory of BeanFactory:

public interface ListableBeanFactory extends BeanFactory {boolean containsBeanDefinition(String beanName);int getBeanDefinitionCount();String[] getBeanDefinitionNames();String[] getBeanNamesForType(Class
  type);String[] getBeanNamesForType(Class
  type, boolean includeNonSingletons, boolean allowEagerInit);
 
   Map
  
    getBeansOfType(Class
   
     type) throws BeansException;
    
      Map
     
       getBeansOfType(Class
      
        type, boolean includeNonSingletons, boolean allowEagerInit)throws BeansException;String[] getBeanNamesForAnnotation(Class
        annotationType);Map
       
         getBeansWithAnnotation(Class
         annotationType) throws BeansException; A findAnnotationOnBean(String beanName, Class annotationType)throws NoSuchBeanDefinitionException;}
       
      
     
    
   
  
 

Officially, this class is used to enumerate classes with xml definitions, such as xml-based BeanFactory like XmlBeanFactory. It only queries the beans created through its own channel and ignores the beans created through other channels (such as HierarchicalBeanFactory ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.