(笔记)Spring实战_装配Bean(2)_注入Bean属性

Spring可以借助属性的set方法来配置属性的值,以实现setter方式的注入。

package com.springinaction.springidol;

public interface Instrument
{
    public void play();
}
package com.springinaction.springidol;

public class Instrumentalist implements Performer
{

    public void perform()
    {
        System.out.println("Playing " + song + " : ");
        instrument.play();
    }

    private String song;

    // 注入歌曲
    public void setSong(String song)
    {
        this.song = song;
    }

    private Instrument instrument;

    // 注入乐器
    public void setInstrument(Instrument instrument)
    {
        this.instrument = instrument;
    }

}
    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist" />

1.注入简单值

    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells" />
    </bean>

<property>元素并没有限制只能注入String类型的值,value属性同样可以指定数值类型(int、float、java.lang.Double等)以及boolean型的值。
注意value属性的使用,为它设置数字类型的值和设置String类型的值并没有任何不同。Spring将根据Bean属性的类型自动判断value值的正确类型。
2.引入其他Bean

package com.springinaction.springidol;

public class Saxophone implements Instrument
{
    public Saxophone()
    {

    }

    public void play()
    {
        System.out.println("TOOT TOOT TOOT");
    }
}
    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells" />
        <property name="instrument" ref="saxophone" />
    </bean>

    <bean id="saxophone" class="com.springinaction.springidol.Saxophone" />

内部Bean

    <bean id="jenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells" />
        <property name="instrument">
            <bean class="com.springinaction.springidol.Saxophone" />
        </property>
    </bean>

内部Bean是通过直接声明一个<bean>元素作为<property>元素的子节点而定义的。
内部Bean并不仅限于setter注入,我们还可以把内部Bean装配到构造方法的入参中。

    <bean id="poeticBuke" class="com.springinaction.springidol.PoeticJuggler">
        <constructor-arg value="15" />
        <constructor-arg>
            <bean class="com.springinaction.springidol.Sonnet29" />
        </constructor-arg>
    </bean>

内部Bean仅适用于一次注入,而且也不能被其他Bean所用。
3.使用Spring的命名空间p装配属性
需要在Spring的XML配置中增加如下一段声明:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="henny" class="com.springinaction.springidol.Instrumentalist"
        p:song="Jingle Bells" p:instrument-ref="saxophone" />

-ref后缀作为一个标识来告知Spring应该装配一个引用而不是字面量。
4.装配集合

集合元素用途
<list>装配list类型的值,允许重复
<set>转配set类型的值,不允许重复
<map>装配map类型的值,名称和值可以是任意类型
<props>装配properties类型的值,名称和值必须都是String类型

当装配类型为数组或者java.util.Collection任意实现的属性时,<list><set>元素非常有用。
<map><props>这两个元素分别对应java.util.Map和java.util.Properties。

package com.springinaction.springidol;


import java.util.Collection;


public class OneManBand implements Performer
{

    public OneManBand()
    {

    }

    public void perform()
    {
        for (Instrument instrument : instruments)
        {
            instrument.play();
        }
    }

    private Collection<Instrument> instruments;

    public void setInstruments(Collection<Instrument> instruments)
    {
        this.instruments = instruments;
    }

}

装配List、Set和Array

    <bean id="hank" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <list>
                <ref bean="piano" />
                <ref bean="saxophone" />
            </list>
        </property>
    </bean>

<ref>元素用来定义Spring上下文中的其他Bean引用。
还可以使用其他的Spring设值元素作为<list>的成员,包括<value><bean><null/>。实际上,<list>可以包含另外一个<list>作为其成员,形成多维列表。
如果Bean的属性类型为数组类型或java.util.Collection接口的任意实现,都可以使用<lsit>元素:private java.util.List<Instrument> instruments;private Instrument[] instruments;
再次说明,无论<list>还是<set>都可以用来装配类型为java.util.Collection的任意实现或者数组的属性。不能因为属性为java.util.Set类型,就表示用户必须使用<set>元素来完成装配。
需要确保List中的每一个成员都必须是唯一的。

    <bean id="tank" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <set>
                <ref bean="piano" />
                <ref bean="saxophone" />
                <ref bean="piano" />
            </set>
        </property>
    </bean>

装配Map集合

package com.springinaction.springidol;


import java.util.Map;


public class OneManOrch implements Performer
{

    public OneManOrch()
    {

    }

    public void perform()
    {
        for (String key : instruments.keySet())
        {
            System.out.print(key + ":");
            Instrument instrument = instruments.get(key);
            instrument.play();
        }
    }

    private Map<String, Instrument> instruments;

    public void setInstruments(Map<String, Instrument> instruments)
    {
        this.instruments = instruments;
    }

}
    <bean id="gank" class="com.springinaction.springidol.OneManOrch">
        <property name="instruments">
            <map>
                <entry key="PIANO" value-ref="piano" />
                <entry key="SAXOPHONE" value-ref="saxophone" />
            </map>
        </property>
    </bean>
属性用途
key指定map中entry的键为String
key-ref指定map中entry的键为Spring上下文中其他Bean的引用
value指定map中entry的值为String
value-ref指定map中entry的值为Spring上下文中其他Bean的引用

装配Properties集合

package com.springinaction.springidol;


import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;


public class OneManOrchestra implements Performer
{

    public OneManOrchestra()
    {

    }

    public void perform()
    {
        Iterator<Entry<Object, Object>> it = instruments.entrySet().iterator();
        while (it.hasNext())
        {
            Entry<Object, Object> entry = it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + ":" + value);
        }
    }

    private Properties instruments;

    public void setInstruments(Properties instruments)
    {
        this.instruments = instruments;
    }

}
    <bean id="mank" class="com.springinaction.springidol.OneManOrchestra">
        <property name="instruments">
            <props>
                <prop key="PINAO">PLINK PLINK PLINK</prop>
                <prop key="SAXOPHONE">TOOT TOOT TOOT</prop>
            </props>
        </property>
    </bean>

5.装配空值
<property name="someNonNullProperty"><null/></property>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值