Spring的核心技术(八)---依赖注入的示例(1)

来源:互联网 发布:炫踪网络怎么样 编辑:程序博客网 时间:2024/06/05 01:19

下面的示例使用了基于XML的配置元数据来配置基于Setter方法的依赖注入。这只是Spring的XML配置文件所指定的Bean定义的一小部分。

<bean id="exampleBean" class="examples.ExampleBean">
    <!-- setter injection using the nested ref element -->
    <property name="beanOne">
        <ref bean="anotherExampleBean"/>
    </property>
 
    <!-- setter injection using the neater ref attribute -->
    <property name="beanTwo" ref="yetAnotherBean"/>
    <property name="integerProperty" value="1"/>
</bean>
 
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

 

public class ExampleBean {
 
    private AnotherBean beanOne;
    private YetAnotherBean beanTwo;
    private int i;
 
    public void setBeanOne(AnotherBean beanOne) {
        this.beanOne = beanOne;
    }
 
    public void setBeanTwo(YetAnotherBean beanTwo) {
        this.beanTwo = beanTwo;
    }
 
    public void setIntegerProperty(int i) {
        this.i = i;
    }
 
}

 

0 0