美文网首页
依赖查找和依赖注入

依赖查找和依赖注入

作者: 文萃北 | 来源:发表于2021-02-28 16:12 被阅读0次

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

依赖查找:容器中的对象通过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<User> users; // 自定义 Bean

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

        private ObjectFactory<ApplicationContext> objectFactory;


        public Collection<User> getUsers() {
            return users;
        }

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


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

        public BeanFactory getBeanFactory() {
            return beanFactory;
        }

        public ObjectFactory<ApplicationContext> getObjectFactory() {
            return objectFactory;
        }

        public void setObjectFactory(ObjectFactory<ApplicationContext> 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 实现了。

相关文章

  • 依赖查找和依赖注入

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

  • 第三章 在spring中引入IoC和DI

    概念关系 控制反转(IoC) VS 依赖注入(DI) 控制反转可以分为两种子类型:依赖注入(DI)和依赖查找 1....

  • 依赖查找和依赖注入的来源区别

    其实这两个的最大区别是,依赖注入的来源可以是非spring容器管理的对象 在refresh()启动容器的时候,会调...

  • Spring系列(1)-装配Spring Bean

    本章的学习目标看如下的思维导图: 依赖注入 在实际环境中实现Ioc容器的方式主要分为:依赖查找和依赖注入二者关系 ...

  • Dagger2常用注解诠释

    依赖注入 控制反转(IoC)与依赖注入(DI)浅谈依赖注入理解依赖注入(IOC)和学习Unity Gradle配置...

  • 依赖注入和依赖注入容器

    依赖注入是一种常见的设计模式,在合适的时候使用它,可以提高我们代码的质量。依赖注入是控制反转的一种实现,那么什么是...

  • JAVA IOC 与 DI

    依赖倒置、控制反转和依赖注入的区分 依赖倒置、控制反转和依赖注入的区分依赖倒置(Dependency Invers...

  • 开源项目的依赖注入

    开源项目的依赖注入 依赖注入概念 依赖注入(DI:Dependency Injection): 依赖注入方式: B...

  • 第三章 3.1DI的配置

    3.1 依赖和依赖注入 依赖注入带来的好处: 动态替换Bean依赖对象,程序更灵活:替换Bean依赖对象,无需修改...

  • 浅析Spring IOC、依赖注入(DI)和依赖查找(DL)

    为什么要用IOC? 第一:对象的实例化不是一件简单的事情,比如对象的关系比较复杂,依赖关系往往需要程序员去维护,这...

网友评论

      本文标题:依赖查找和依赖注入

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