美文网首页
org.springframework.beans.factor

org.springframework.beans.factor

作者: moralok | 来源:发表于2020-04-15 18:24 被阅读0次
    package org.springframework.beans.factory;
    
    import org.springframework.beans.BeansException;
    
    /**
     * Interface to be implemented by beans that wish to be aware of their
     * owning {@link BeanFactory}.
     *
     * <p>For example, beans can look up collaborating beans via the factory
     * (Dependency Lookup). Note that most beans will choose to receive references
     * to collaborating beans via corresponding bean properties or constructor
     * arguments (Dependency Injection).
     *
     * <p>For a list of all bean lifecycle methods, see the
     * {@link BeanFactory BeanFactory javadocs}.
     *
     * @author Rod Johnson
     * @author Chris Beams
     * @since 11.03.2003
     * @see BeanNameAware
     * @see BeanClassLoaderAware
     * @see InitializingBean
     * @see org.springframework.context.ApplicationContextAware
     */
    public interface BeanFactoryAware extends Aware {
    
        /**
         * Callback that supplies the owning factory to a bean instance.
         * <p>Invoked after the population of normal bean properties
         * but before an initialization callback such as
         * {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
         * @param beanFactory owning BeanFactory (never {@code null}).
         * The bean can immediately call methods on the factory.
         * @throws BeansException in case of initialization errors
         * @see BeanInitializationException
         */
        void setBeanFactory(BeanFactory beanFactory) throws BeansException;
    
    }
    
    

    简介

    org.springframework.beans.factory.Aware 的一个子接口。

    文档

    由希望知道自己的 BeanFactorybean 实现的接口。
    例如,bean 可以通过工厂(依赖查找)来查找 协作bean。 注意,大多数 bean 将选择通过相应的 bean属性 或构造函数参数(依赖注入)来接收对 协作bean 的引用。

    有关所有 bean生命周期 方法的列表,请参见 BeanFactory javadocs

    思考

    协作 Bean 指代的应该就是这个 Bean 依赖的那些 Bean。比如一个 Robert Bean 依赖于 Action Bean
    按文档所言,在 Spring 中我们通常会有以下两种方式来实现这个依赖关系:

    1. 属性
    public class Robert {
    
        // 使用 @Autowired 注解实现在 Spring 容器中查找对应的 Action Bean 并赋值给该属性
        @Autowired
        private Action action;
    
        public void walk() {
            action.walk();
        }
    }
    
    1. 构造器
    public class Robert {
    
        private Action action;
    
        // 使用 @Autowired 注解实现在 Spring 容器中查找对应的 Action Bean 并赋值给该参数
        @Autowired
        Robert(Action action) {
            this.action = action;
        }
    
        public void walk() {
            action.walk();
        }
    }
    

    我们可以在实现了 BeanFactoryAware 接口的 Robert BeansetBeanFactory 获得对应的 BeanFactory。假设我们之前在一个实现了 BeanNameAwareAction BeansetBeanName 方法中知道了它的 nameheyThisIsNickname。我们可以通过 BeanFactory#get(String name) throws BeansException 方法尝试获取 Action Bean 从而去实现某个意图。

    相关文章

      网友评论

          本文标题:org.springframework.beans.factor

          本文链接:https://www.haomeiwen.com/subject/armqvhtx.html