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 getBean(String name, Class requiredType) throws BeansException;
    Object getBean(String name, Object... args) throws BeansException;
     T getBean(Class requiredType) throws BeansException;
     T getBean(Class requiredType, Object... args) throws BeansException;
     ObjectProvider getBeanProvider(Class requiredType);
     ObjectProvider 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 getBean(String name, Class requiredType) throws BeansException;
根据名称和类型获取Bean
  1. Object getBean(String name, Object... args) throws BeansException;
通过name和对象参数获取Bean
  1. T getBean(Class requiredType) throws BeansException;
通过类型获取Bean
  1. T getBean(Class requiredType, Object... args) throws BeansException;
通过类型和参数获取Bean
  1. ObjectProvider getBeanProvider(Class requiredType);
返回指定ObjectProvider,提供延迟加载功能
  1. ObjectProvider 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则提供单一类型,集合类型和层次性的依赖查找能力.

WechatIMG8.png

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

你可能感兴趣的:(java,spring)