转载

BeanFactory和ObjectFactory的区别

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 = "&"; 这句代码定义在BeanFactory这个最顶级的接口中。他的作用是如果在使用beanName获取Bean时,在BeanName前添加这个前缀, 那么使用这个BeanName获得的Bean实例是其所在FactoryBean的实例,也就是实现 FactoryBean 接口的那个类的Bean实例。

  1. Object getBean(String name) throws BeansException;

通过Bean名称获取Bean

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

根据名称和类型获取Bean

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

通过name和对象参数获取Bean

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

通过类型获取Bean

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

通过类型和参数获取Bean

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

返回指定ObjectProvider,提供延迟加载功能

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

Class,Field,Method 类型的延迟加载功能

  1. boolean containsBean(String name);

容器中是否包含名字为name的Bean

  1. boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

Bean是否是单例

  1. boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

判断bean是不是原型

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

检查具有给定名称的bean是否与指定的类型(Class,Field,Method 类型)匹配。

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

检查具有给定名称的bean是否与指定的类型匹配。

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

确定具有给定名称的bean的类型

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

确定具有给定名称的bean的类型

  1. String[] getAliases(String name);

返回给定bean名称的别名

ObjectFactory

package javax.naming.spi;

import java.util.Hashtable;

import javax.naming.*;


public interface ObjectFactory {
    //为指定对象和环境创建一个对象实例
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
                                    Hashtable<?,?> environment)
        throws Exception;
}

相同点

BeanFactory和ObjectFactory都提供依赖查找的能力

区别

  • BeanFactory实现了ObjectFactory接口
  • ObjectFactory仅仅关注一个或者一种类型Bean的查找,并且自身不具备依赖查找的能力.
  • BeanFactory则提供单一类型,集合类型和层次性的依赖查找能力.

BeanFactory和ObjectFactory的区别

原文: https://rumenz.com/rumenbiji/Spring-BeanFactory-ObjectFactory.html

原文  https://segmentfault.com/a/1190000023213575
正文到此结束
Loading...