依赖查找和依赖注入

依赖查找和依赖注入的区别

依赖查找:容器中的对象通过Api来查找自己所依赖的资源和对象

    private static void lookupInRealTime(BeanFactory beanFactory) {
        User user = (User) beanFactory.getBean("user");
        System.out.println("实时查找:" + user);
    }

依赖注入:依赖对象和注入对象统一由容器进行管理,由IOC Service Provider 进行统一管理。有三种方式,构造方法注入,set方法注入。接口注入或者是注解方法注入。

Bean 的来源

  • 自定义Bean
  • 内建依赖
    public class UserRepository {

        private Collection users; // 自定义 Bean

        private BeanFactory beanFactory; // 內建非 Bean 对象(依赖)

        private ObjectFactory objectFactory;


        public Collection getUsers() {
            return users;
        }

        public void setUsers(Collection users) {
            this.users = users;
        }


        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }

        public BeanFactory getBeanFactory() {
            return beanFactory;
        }

        public ObjectFactory getObjectFactory() {
            return objectFactory;
        }

        public void setObjectFactory(ObjectFactory objectFactory) {
            this.objectFactory = objectFactory;
        }
}
  • 容器内建Bean
    Environment environment = applicationContext.getBean(Environment.class);
    System.out.println("获取 Environment 类型的 Bean:" + environment);

内建Bean BeanFactory 注入的方式

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/
    dependency-injection-context.xml");
    UserRepository userRepository = applicationContext.getBean("userRepository", UserRepository.class);
    System.out.println(userRepository.getBeanFactory()) ;
result :

    org.springframework.beans.factory.support.DefaultListableBeanFactory@11438d26: defining beans [user,superUser,objectFactory,userRepository]; root of factory hierarchy

从上面代码可以看出 userRepository 中的 BeanFactory 是内建Bean, 是通过 DefaultListableBeanFactory 来实现的。
ClassPathXmlApplicationContext 继承自 AbstractRefreshableApplicationContext ,AbstractRefreshableApplicationContext 中定义了一个 DefaultListableBeanFactory 对象。

ApplicationContext 和 BeanFactory之间的关系。

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It adds:

  • Easier integration with Spring’s AOP features
  • Message resource handling (for use in internationalization)
  • Event publication
  • Application-layer specific contexts such as the WebApplicationContext for use in web applications.
    In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s IoC container. For more information on using the BeanFactory instead of the ApplicationContext, see The BeanFactory.

ApplicationContext 是 BeanFactory 的一个子接口,为了更好的实现 Aop, 消息国际化 ,事件,应用层特定的上下文。Application提供了更加企业化的功能,ApplicationContext 是 BeanFactory的 超集。

//问题 1 :依赖查找和依赖注入的源头分别来自哪里。
//问题 2 :依赖查找和依赖注入的区别。
//问题 3 : 内建Bean BeanFactory 是如何注入的,为啥是通过DefaultListableBeanFactory 实现了。

你可能感兴趣的:(依赖查找和依赖注入)