Spring IOC 和 DI

(1)什么是IOC(Inversion Of Control 控制反转)?

对象之间的依赖关系应该由容器来建立。

 

(2)什么是DI(Dependency Injection 依赖注入)?

容器可以通过调用set方法或者构造器来建立对象之间的依赖关系。

 

注:IOC是目标,DI是手段。

 

(3)使用set方法来注入

配置set方法注入。property元素的name属性用于指定set方法名(首字母大写,前面添加set),ref属性用于指定被注入的bean的id。

<bean id="a1" class="ioc.A">

  <property name="b" ref="b1"/>

</bean>

(4)使用构造器来注入

<bean id="wt1" class="ioc.Waiter"/>

 

配置构造器注入。constructor-arg的index属性用来指定参数的下标,ref属性用来指定被注入的bean的id。

 

<bean id="rest1" class="ioc.Restaurant">

  <constructor-arg index="0" ref="wt1"/>

</bean>

 

 

(5)自动装配

a.自动装配指的是,容器按照某种规则,自动建立对象之间的依赖关系。

即不用明确指定被注入的bean的id,容器底层仍然需要调用set方法或者构造器来完成依赖关系的建立。

 

b.容器默认情况下,是不会自动装配的。

<bean id="eg1" class="autowire.Enginee"/>

 

autowire属性用于指定自动装配的方式,有三个值:

byName: 容器以属性名作为beanid来查找对应的bean,找到之后,调用set方法来完成注入。

注:如果找不到对应的bean,则不注入。

 

byType:容器以属性类型作为bean的类型来查找对应的bean,找到之后,调用set方法来完成注入。

注:如果找不到对应的bean,则不注入。如果找到多个,则会出错。

 

constructor:类似byType,只不过调用构造器来完成注入。

 

<bean id="car" class="autowire.Car" autowire="byType"/>

(6)注入基本类型的值

注入基本类型的值(包括String),使用value属性。


    注入基本类型的值(包括String),使用value属性。
     

<bean id="vb1" class="value.ValueBean">
    <property name="name" value="花花"/>
    <property name="age" value="20"/>
</bean>

(7)注入集合类型的值

    <bean id="vb1" class="value.ValueBean">
        <property name="name" value="花花"/>
        <property name="age" value="20"/>
        <property name="interest">
            <list>
                <value>钓鱼</value>
                <value>做饭</value>
                <value>看电视</value>
                <value>看电视</value>
            </list>
        </property>
        <property name="city">
            <set>
                <value>北京</value>
                <value>洛阳</value>
                <value>南京</value>
            </set>
        </property>
        <property name="score">
            <map>
                <entry key="english" value="60"/>
                <entry key="math" value="1"/>
            </map>
        </property>
        <property name="db">
            <props>
                <prop key="username">Tom</prop>
                <prop key="password">1234</prop>
            </props>
        </property>
    </bean>

 

 

 

(8)引用的方式注入集合类型的值

 

    <util:list id="interestBean">
        <value>抽烟</value>
        <value>喝酒</value>
        <value>烫头</value>
    </util:list> 
    <util:set id="cityBean">
        <value>长沙</value>
        <value>岳阳</value>
        <value>华容</value>
    </util:set>
    <util:map id="scoreBean">
        <entry key="english" value="90"/>
        <entry key="math" value="80"/>
    </util:map>
    <util:properties id="dbBean">
        <prop key="username">Sally</prop>
        <prop key="password">test</prop>
    </util:properties>
    <bean id="vb2" class="value.ValueBean">
        <property name="interest" ref="interestBean"/>
        <property name="city" ref="cityBean"/>
        <property name="score" ref="scoreBean"/>
        <property name="db" ref="dbBean"/>
    </bean>

 

 

(9)读取properties文件的内容

读取.properties文件的内容。location属性指定properties文件的位置,容器会读取指定文件的内容,然后将这些内容添加到了Properties对象里面。

 

<util:properties id="config" location="classpath:config.properties"/>

 

 

(10)Spring表达式

猜你喜欢

转载自www.cnblogs.com/wood-life/p/10289053.html