The difference between beanfactory and objectfactory

Time:2021-1-19

BeanFactory

package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
public interface BeanFactory {

    String FACTORY_BEAN_PREFIX = "&";
    Object getBean(String name) throws BeansException;
    <T> T getBean(String name, Class<T> requiredType) throws BeansException;
    Object getBean(String name, Object... args) throws BeansException;
    <T> T getBean(Class<T> requiredType) throws BeansException;
    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
    <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
    <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
    boolean containsBean(String name);
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
    boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
    boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
    @Nullable
    Class<?> getType(String name) throws NoSuchBeanDefinitionException;
    @Nullable
    Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
    String[] getAliases(String name);

}
  1. String FACTORY_BEAN_PREFIX = “&”;

String FACTORY_ BEAN_ Prefix = “&”; this code is defined in the top-level interface of beanfactory. Its function is to add this prefix before the bean name when using the bean name to get the bean, then the bean instance obtained by using the bean name is the instance of the factorybean, that is, the bean instance of the class that implements the factorybean interface.

  1. Object getBean(String name) throws BeansException;

Get bean by bean name

  1. <T> T getBean(String name, Class<T> requiredType) throws BeansException;

Get bean by name and type

  1. Object getBean(String name, Object… args) throws BeansException;

Get bean by name and object parameters

  1. <T> T getBean(Class<T> requiredType) throws BeansException;

Get bean by type

  1. <T> T getBean(Class<T> requiredType, Object… args) throws BeansException;

Get bean by type and parameter

  1. <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);

Returns the specified objectprovider to provide the delay loading function

  1. <T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);

Delay loading function of class, field and method

  1. boolean containsBean(String name);

Does the container contain a bean named name

  1. boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

Is bean a singleton

  1. boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

Determining whether a bean is a prototype

  1. boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;

Checks whether the bean with the given name matches the specified type (class, field, method type).

  1. boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;

Checks whether the bean with the given name matches the specified type.

  1. Class<?> getType(String name) throws NoSuchBeanDefinitionException;

Determines the type of bean with the given name

  1. Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;

Determines the type of bean with the given name

  1. String[] getAliases(String name);

Returns the alias of the given bean name

ObjectFactory

package javax.naming.spi;

import java.util.Hashtable;

import javax.naming.*;


public interface ObjectFactory {
    //Creates an object instance for the specified object and environment
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                                    Hashtable<?,?> environment)
        throws Exception;
}

Similarities

Both beanfactory and objectfactory provide the ability to rely on lookup

difference

  • Beanfactory implements the objectfactory interface
  • Objectfactory only focuses on the search of one or one type of bean, and does not have the ability to rely on search
  • Beanfactory provides single type, collection type and hierarchical dependency search capabilities

The difference between beanfactory and objectfactory

Original text: https://rumenz.com/rumenbiji/Spring-BeanFactory-ObjectFactory.html