• Spring IOC


    Spring IOC简介

               IOC 全称 Inversion of Control 译:控制反转也可以叫做依赖注入,对象仅仅通过自己的构造函数参数、工厂方法参数或工厂方法返回的对象来定义依赖项(也就是关联对象),然后当IOC容器初始化bean的时候进行注入依赖项的整个过程叫做控制反转,这种方法从根本上反转了bean直接通过构造或ServiceLocator机制实例化或定位依赖项的机制创建对象(所以叫控制反转)

    org.springframework.beansorg.springframework.context包是Spring框架的IoC容器的基础,BeanFactory接口提供了一种高级配置机制,能够管理任何类型的对象。ApplicationContextBeanFactory的子类

    扩展了BeanFactory功能:更容易与Spring的AOP功能集成、消息资源处理(用于国际化)、事件的发布、应用层特定的上下文,如用于web应用程序的WebApplicationContext。

    Spring IOC容器简介

      org.springframework.context.ApplicationContext接口表示Spring IoC容器,负责实例化,配置和注入bean,容器通过读取配置元数据,获取其中配置确定的依赖项关系来实例化、配置、注入bean,如图:

    配置元数据

     配置元数据有三种方式,传统的XML方式,java方式和注解方式。

    1、 XML方式: 

     配置a.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="..." class="...">   
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <bean id="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <!-- more bean definitions go here -->
    
    </beans>

    id:单个bean的定义,唯一

    calss:该bean的类型,使用全限定名

    实例化容器并读取a.xml相关配置以及使用容器获取初始化的bean对象(多个文件用逗号分隔)

    ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
    // retrieve configured instance
    //第一个参数bean的id,第二个参数bean的类型 PetStoreService service = context.getBean("petStore", PetStoreService.class); // use configured instance List<String> userList = service.getUsernameList();

    待续...

       

  • 相关阅读:
    Try out the latest C++ compiler toolset without waiting for the next update of Visual Studio
    MAC OS X 系统怎么样?
    java多线程总结
    软件开发的一些概念
    原生JS投票特效
    PHP+MySql例子
    CSS控制文本超出指定宽度显示省略号和文本不换行
    android图片拖动缩放
    jQuery带控制按钮向上和向下滚动文本列表
    HTML5 语义元素
  • 原文地址:https://www.cnblogs.com/shiguotao-com/p/10156909.html
Copyright © 2020-2023  润新知