Spring源码阅读-DefaultListableBeanFactory的属性注释翻译

本文深入介绍了Spring框架中的DefaultListableBeanFactory,这是一个基于bean定义对象的成熟bean工厂实现。它支持bean定义的注册与查找,并提供了多种配置选项,如允许bean定义覆盖、允许懒加载bean的早期类加载等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纯属个人翻译,英语未过四级,有错包含/指教。。。。

package org.springframework.beans.factory.support;

/**
 * Default implementation of the 默认实现
 * {@link org.springframework.beans.factory.ListableBeanFactory} and
 * {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory
 * based on bean definition objects. 一个成熟的bean工厂,基于bean定义对象
 *
 * <p>Typical usage is registering all bean definitions first (possibly read
 * 典型的用法是在访问beans之前先注册所有的Bean(可能读取自一个bean定义的文件)定义(就是先把所有的BEAN初始化,而后可以直接使用)
 * from a bean definition file), before accessing beans. Bean definition lookup
 * 因为操作预先创建的bean定义元数据对象,在本地bean 所以定义表中查找成为一种廉价的操作
 * is therefore an inexpensive operation in a local bean definition table,
 * operating on pre-built bean definition metadata objects.
 *
 * <p>Can be used as a standalone bean factory, or as a superclass for custom
 * 可以作为一个单独的bean工厂使用,也可以作为一些定制的bean工厂使用,
 * bean factories. Note that readers for specific bean definition formats are
 * 在这里要注意:那些指定bean定义格式的readers是分开单独实现 的
 * typically implemented separately rather than as bean factory subclasses:
 * 而不是作为bean工厂的子类,例如查看:
 * see for example {@link PropertiesBeanDefinitionReader} and
 * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
 *
 * <p>For an alternative implementation of the
 * 二选一的,此类实现了ListableBeanFactory接口,
 * {@link org.springframework.beans.factory.ListableBeanFactory} interface,
 * 可以看一下StaticListableBeanFactory接口,它在管理已存在bean实例的时候,
 * have a look at {@link StaticListableBeanFactory}, which manages existing
 * 并不是根据bean定义创建一个新的bean
 * bean instances rather than creating new ones based on bean definitions.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Costin Leau
 * @author Chris Beams
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @since 16 April 2001
 * @see StaticListableBeanFactory
 * @see PropertiesBeanDefinitionReader
 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 */
@SuppressWarnings("serial")
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {

    private static Class<?> javaxInjectProviderClass = null;

    static {
        try {
            javaxInjectProviderClass =
                    ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());
        }
        catch (ClassNotFoundException ex) {
            // JSR-330 API not available - Provider interface simply not supported then.
        }
    }


    /** 序列化ID映射到工厂实例的Map:Map from serialized id to factory instance */
    private static final Map<String, Reference<DefaultListableBeanFactory>> serializableFactories =
            new ConcurrentHashMap<>(8);

    /** 可选ID,用于序列化:Optional id for this factory, for serialization purposes */
    private String serializationId;

    /** Whether to allow re-registration of a different definition with the same name */
    /** 是否允许同名字不同定义的bean重新注册*/
    private boolean allowBeanDefinitionOverriding = true;

    /** Whether to allow eager class loading even for lazy-init beans */
    /** 是否允许bean基至是延迟加载的bean马上加载*/
    private boolean allowEagerClassLoading = true;

    /** Optional OrderComparator for dependency Lists and arrays */
    /** 可选的用于依赖集合和数组的比较器*/
    private Comparator<Object> dependencyComparator;

    /** Resolver to use for checking if a bean definition is an autowire candidate */
    /** 用于检查一个类定义是否有自动注入请求的解析器*/
    private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();

    /** Map from dependency type to corresponding autowired value */
    /** 依赖类型到对应的注入值的映射*/
    private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);

    /** Map of bean definition objects, keyed by bean name */
    /** bean定义名字到bean定义数据实体的映射*/
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

    /** Map of singleton and non-singleton bean names, keyed by dependency type */
    /** 依赖类型到对应bean名字的映射,包括单例和非单例*/
    private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);

    /** Map of singleton-only bean names, keyed by dependency type */
    /** 依赖类型到对应bean名字的映射,仅包含单例*/
    private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);

    /** List of bean definition names, in registration order */
    /** 所有bean定义的名字的集合,按注册次序*/
    private volatile List<String> beanDefinitionNames = new ArrayList<>(256);

    /** List of names of manually registered singletons, in registration order */
    /** 手工注册的单例bean定义的名字的集合,按注册次序*/
    private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);

    /** Cached array of bean definition names in case of frozen configuration */
    /** 缓存bean定义名字的数组*/
    private volatile String[] frozenBeanDefinitionNames;

    /** Whether bean definition metadata may be cached for all beans */
    /** 是否允许bean定义的元数据被缓存,以用于所有的bean*/
    private volatile boolean configurationFrozen = false;


    /**
     * Create a new DefaultListableBeanFactory.
     */
    public DefaultListableBeanFactory() {
        super();
    }

    /**
     * Create a new DefaultListableBeanFactory with the given parent.
     * @param parentBeanFactory the parent BeanFactory
     */
    public DefaultListableBeanFactory(BeanFactory parentBeanFactory) {
        super(parentBeanFactory);
    }


    /**
     * Specify an id for serialization purposes, allowing this BeanFactory to be
     * 指定一个ID用于序列化操作,如有需要允许这个BeanFactory通过这个ID反序化为此BeanFactory对象
     * deserialized from this id back into the BeanFactory object, if needed.
     */
    public void setSerializationId(String serializationId) {
        if (serializationId != null) {
            serializableFactories.put(serializationId, new WeakReference<>(this));
        }
        else if (this.serializationId != null) {
            serializableFactories.remove(this.serializationId);
        }
        this.serializationId = serializationId;
    }

    /**
     * Return an id for serialization purposes, if specified, allowing this BeanFactory
     * to be deserialized from this id back into the BeanFactory object, if needed.
     * @since 4.1.2
     */
    public String getSerializationId() {
        return this.serializationId;
    }

    /**
     * Set whether it should be allowed to override bean definitions by registering
     * a different definition with the same name, automatically replacing the former.
     * If not, an exception will be thrown. This also applies to overriding aliases.
     * <p>Default is "true".
     * @see #registerBeanDefinition
     */
    public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
        this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
    }

    /**
     * Return whether it should be allowed to override bean definitions by registering
     * a different definition with the same name, automatically replacing the former.
     * @since 4.1.2
     */
    public boolean isAllowBeanDefinitionOverriding() {
        return this.allowBeanDefinitionOverriding;
    }

    /**
     * Set whether the factory is allowed to eagerly load bean classes
     * 设置该工厂是否被允许马上加载bean定义
     * even for bean definitions that are marked as "lazy-init".
     * 即使是被标记为延迟加载的bean定义
     * <p>Default is "true". Turn this flag off to suppress class loading
     * for lazy-init beans unless such a bean is explicitly requested.
     * 如果有明确的要求,可以关闭这个标志以阻止延迟加载的类加载
     * In particular, by-type lookups will then simply ignore bean definitions
     * without resolved class name, instead of loading the bean classes on
     * demand just to perform a type check.
     * 尤其是by-type查找的时候总是忽略bean的这此定义,不解析类名,而是根据需要加载bean仅仅是完成一个类型检查
     * @see AbstractBeanDefinition#setLazyInit
     */
    public void setAllowEagerClassLoading(boolean allowEagerClassLoading) {
        this.allowEagerClassLoading = allowEagerClassLoading;
    }

    /**
     * Return whether the factory is allowed to eagerly load bean classes
     * even for bean definitions that are marked as "lazy-init".
     * @since 4.1.2
     */
    public boolean isAllowEagerClassLoading() {
        return this.allowEagerClassLoading;
    }

    /**
     * Set a {@link java.util.Comparator} for dependency Lists and arrays.
     * @see org.springframework.core.OrderComparator
     * @see org.springframework.core.annotation.AnnotationAwareOrderComparator
     */
    public void setDependencyComparator(Comparator<Object> dependencyComparator) {
        this.dependencyComparator = dependencyComparator;
    }

    /**
     * Return the dependency comparator for this BeanFactory (may be {@code null}.
     */
    public Comparator<Object> getDependencyComparator() {
        return this.dependencyComparator;
    }

    /**
     * Set a custom autowire candidate resolver for this BeanFactory to use
     * when deciding whether a bean definition should be considered as a
     * candidate for autowiring.
     * 给这个工厂设置一个定制的自动注入请求解析器,以便当决定是否一个bean的定义可以考虑为自动注入请求时使用
     */
    public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
        Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
        if (autowireCandidateResolver instanceof BeanFactoryAware) {
            if (System.getSecurityManager() != null) {
                final BeanFactory target = this;
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(target);
                        return null;
                    }
                }, getAccessControlContext());
            }
            else {
                ((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
            }
        }
        this.autowireCandidateResolver = autowireCandidateResolver;
    }

    /**
     * Return the autowire candidate resolver for this BeanFactory (never {@code null}).
     */
    public AutowireCandidateResolver getAutowireCandidateResolver() {
        return this.autowireCandidateResolver;
    }


    @Override
    public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
        super.copyConfigurationFrom(otherFactory);
        if (otherFactory instanceof DefaultListableBeanFactory) {
            DefaultListableBeanFactory otherListableFactory = (DefaultListableBeanFactory) otherFactory;
            this.allowBeanDefinitionOverriding = otherListableFactory.allowBeanDefinitionOverriding;
            this.allowEagerClassLoading = otherListableFactory.allowEagerClassLoading;
            this.autowireCandidateResolver = otherListableFactory.autowireCandidateResolver;
            this.resolvableDependencies.putAll(otherListableFactory.resolvableDependencies);
        }
    }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值