zoukankan      html  css  js  c++  java
  • spring ioc

     1、关于IOC容器

      无spring之前创建对象用传统的new关键字,对象之间的依赖性比较强,耦合度高,自己去管理bean,spring ioc主要是提供了对象容器来管理对象和对象之间的关系,spring ioc叫控制反转或者依赖注入。

    2、IOC实现需要哪些元素

      大家想,如果让你来写一个spring ioc会怎么写?

      首先需要一个容器来装对象(AppliactionContext、BeanFactory),要用的时候直接从AppliactionContext,那对象是怎么创建的呢?举例用spring xml bean来配置对象

      如:

    <bean id="customerVO" class="com.fxr.vo.CustomerVO">
      <property name="name" value="test"></property>
    </bean>

     -----------------------------------------

      Resource

      BeanDefinition 管理基于spring的应用中的各种对象以及它们之间的相互依懒关系,抽象的定义了bean的定义。

      BeanDefinitionReader

      BeanFactory简单实现容器系列,这系列容器只实现了容器的最基本功能。

      ApplicationContext 应用上下文,作为容器的高级形态存在,应用上下文在简单容器的基础之上,增加了许多面向框架的特性,同时对应用环境做了许多适配。

      以上五个都是接口,都有各式各样的实现,正是这5个接口定义了spring ioc容器的基本代码组件结构。而其组件各种实现的组合关系组成了一个运行时的具体容器。

    ClassPathResource cpr = new ClassPathResource("/applicationContext.xml");
    DefaultListableBeanFactory dlb = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader xdr = new XmlBeanDefinitionReader(dlb);
    xdr.loadBeanDefinitions(cpr);
    CustomerVO customerVO
    = (CustomerVO) dlb.getBean("customerVO"); System.out.println(customerVO.getName());

       IOC容器加载简单过程:

      1、Resource定位过程。

      2、BeanDefinition的载入(把用户定义好的Bean表示成IOC容器的内部结构)

      3、向IOC容器注册BeanDefinition的过程。 (把BeanDefinition向IOC进行注册,实际是放入到了HashMap中)

  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/bigwolf/p/4667483.html
Copyright © 2011-2022 走看看