IOC和DI

IOC(Inversion of Control):

其思想是反转资源获取的方向. 

传统的资源查找方式:要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 

而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式

DI(Dependency Injection) — IOC 的另一种表述方式:

即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接

<?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 -->
<bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>


</bean>
</beans>

在spring的loc容器中配置Bean:

通过全类名的方式来配置bean,是通过反射的方式来创建bean的实例,因此在这个类中,

应该有一个无参的构造器。


class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求在Bean中必须有无参数的构造器。

id:标示创建的bean,是唯一的。

在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
Spring 提供了两种类型的 IOC 容器实现. 


BeanFactory: IOC 容器的基本实现.
ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.
BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;

ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
无论使用何种方式, 配置文件时相同的.


ApplicationContext 的主要实现类:
ClassPathXmlApplicationContext:从 类路径下加载配置文件
FileSystemXmlApplicationContext: 从文件系统中加载配置文件


ConfigurableApplicationContext 扩展于 ApplicationContext(是他子接口),

新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力
ApplicationContext 在初始化上下文时就实例化所有单例的 Bean。
WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作


Spring 支持 3 种依赖注入的方式


属性注入:

属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称value 属性或 <value> 子节点指定属性值 
属性注入是实际应用中最常用的注入方式

<property name="name" value="Spring"></property>
构造器注入:

通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性

<bean id="car" class="com.atguigu.spring.beans.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="3000000" type="double"></constructor-arg>
</bean>


<bean id="car2" class="com.atguigu.spring.beans.Car">
<constructor-arg value="Baoma" ></constructor-arg>
<constructor-arg value="ShangHai" ></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>

函数的重载参数的个数,参数的类型

使用构造器注入属性值可以执行参数的文职和参数的类型!以区分重载的构造器




<bean id="car2" class="com.atguigu.spring.beans.Car">
<constructor-arg value="Baoma" ></constructor-arg>
<constructor-arg type="java.lang.String">
<value>
<![CDATA[<ShangHai>^]]></value>
</constructor-arg>
<constructor-arg type="int">
<value>260</value>
</constructor-arg>
</bean>

字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。
基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。
工厂方法注入(很少使用,不推荐)


引用其它 Bean:

组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref  属性为 Bean 的属性或构造器参数指定对 Bean 的引用. 
也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean

<!-- 配置引用类型 -->
<bean id="person" class="com.atguigu.spring.beans.Person">

<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<property name="car" ref="car2"></property>

</bean>


内部bean是不能被外部bean访问的:

<!-- 配置引用类型 -->
<bean id="person" class="com.atguigu.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="18"></property>
<!-- 内部bean -->
<property name="car">
<bean class="com.atguigu.spring.beans.Car">
<constructor-arg value="Fte" index="0"></constructor-arg>
<constructor-arg value="ChangAn" index="1"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg>
</bean>
</property>

</bean>



可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值
和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。

<!-- 测试赋值null -->
 <constructor-arg><null/></constructor-arg>


<bean id="person2" class="com.atguigu.spring.beans.Person">
<constructor-arg value="Hellon"></constructor-arg>
<constructor-arg value="28"></constructor-arg>
<!-- 
<constructor-arg ref="car"></constructor-arg>
 -->
 <!-- 测试赋值null 
 <constructor-arg><null/></constructor-arg>
 -->
 
<constructor-arg ref="car"></constructor-arg>

级联赋值
 <property name="car.maxSpeed" value="20000"></property>

</bean>

为级联属性赋值,注意:属性属性需要先初始化后才可以为级联属性赋值,否则会有异常,和structs2不同


集合属性:

在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
数组的定义和 List 一样, 都使用 <list>
配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.

<!-- 测试如何配置集合属性 -->
<bean id="person3" class="com.atguigu.spring.collections.Person">
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars">
<list>
     <ref bean="car"/>
     <ref bean="car2"/>
     <ref bean="car3"/> 
     <bean id="car3" class="com.atguigu.spring.beans.Car">
<constructor-arg value="KaiMeiRui" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="90000000" type="double"></constructor-arg>
         </bean>
</list>
</property>
</bean>



Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值. 
必须在 <key> 标签里定义键
因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素. 
可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义。

<!-- 配置Map 属性值 -->
<bean id="newperson" class="com.atguigu.spring.collections.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="66"></property>
<property name="cars">
           <map>
           <entry key="AA" value-ref="car"></entry>
           <entry key="BB" value-ref="car2"></entry>
           <entry key="CC" value-ref="car3"></entry>
           </map>
</property>
</bean>


使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.


<!-- 配置Properties属性值 
使用props和prop子节点为properties属性赋值
-->
<bean id="dataSource" class="com.atguigu.spring.collections.DataSource">
      <property name="properties2">
     <props> 
           <prop key="user">root</prop>
           <prop key="password">root</prop>
           <prop key="jdbcUrl">jdbc:mysql:///atguigu</prop>
           <prop key="driverclass">com.mysql.jdbc.Driver</prop>
     </props>
      </property>
</bean>


使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义


<!-- 配置单例的集合bean,以很多个bean进行引用,需要导入util命名空间 -->
    <util:list id="cars">
          <ref bean="car"/>
          <ref bean="car2"/>
          <ref bean="car3"/>
    </util:list>
    
    <bean id="person4" class="com.atguigu.spring.collections.Person">
    <property name="name" value="Jack"></property>
    <property name="age" value="29" ></property>
    <property name="cars" ref="cars"></property>
    </bean>



使用p命名空间:

为了简化 XML 文件的配置,越来越多的 XML 文件采用属性而非子元素配置信息。
Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性。
使用 p 命名空间后,基于 XML 的配置方式将进一步简化

    <!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间 -->
    <bean id="person5" class="com.atguigu.spring.collections.Person"
    p:age="30"
    p:name="Queen"
    p:cars-ref="cars"
    > 








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值