首页 > 学院 > 开发设计 > 正文

[Spring Framework]学习笔记--Dependency injection(DI)

2019-11-14 20:48:11
字体:
来源:转载
供稿:网友
[SPRing Framework]学习笔记--Dependency injection(DI)

1. 通过构造函数实现DI

简单类型实例

package examples;public class ExampleBean {    // Number of years to calculate the Ultimate Answer    private int years;    // The Answer to Life, the Universe, and Everything    private String ultimateAnswer;
    //如果不能在debug模式下进行编译,必须要加下面一行。针对下面的方式3,通过参数名字。  @ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {        this.years = years;        this.ultimateAnswer = ultimateAnswer;    }}

相应的xml配置为

<bean id="exampleBean" class="examples.ExampleBean">  //方式1,通过类型,如果存在两个参数同类型的话,不行。    <constructor-arg type="int" value="7500000"/>    <constructor-arg type="java.lang.String" value="42"/>  //方式2,通过参数位置。
    <constructor-arg index="0" value="7500000"/>    <constructor-arg index="1" value="42"/>
  //方式3,通过参数名字。
    <constructor-arg name="years" value="7500000"/>    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>

对象类型实例

package x.y;public class Foo {    public Foo(Bar bar, Baz baz) {        // ...    }}

xml配置

<beans>    <bean id="foo" class="x.y.Foo">        <constructor-arg ref="bar"/>
        <constructor-arg ref="baz"/>    </bean>    <bean id="bar" class="x.y.Bar"/>    <bean id="baz" class="x.y.Baz"/></beans>

参数也可以像下面这样指定

    <constructor-arg>        <ref bean="bar"/>    </constructor-arg>

如果是通过工厂模式,可以采用下面的方式

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">    <constructor-arg ref="anotherExampleBean"/>    <constructor-arg ref="yetAnotherBean"/>    <constructor-arg value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {    // a private constructor    private ExampleBean(...) {        ...    }    // a static factory method; the arguments to this method can be    // considered the dependencies of the bean that is returned,    // regardless of how those arguments are actually used.    public static ExampleBean createInstance (        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {        ExampleBean eb = new ExampleBean (...);        // some other Operations...        return eb;    }}

2. 通过set方法来实现DI

<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"/>

注:property的name是和set方法中的名字一致的。

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;    }}

总结:在我们日常的工程中,上面两种方式如何使用呢?

可以从需要来看,如果这个属性是必须的,那就放在构造函数中;如果是可选的,那就用set的方式好了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表